Skip to content

cli: show each task's average rate in tasks, not the one of its latest interval - #12117

Open
MrScothh wants to merge 2 commits into
iNavFlight:maintenance-10.xfrom
MrScothh:fix/cli-tasks-average-rate
Open

MrScothh wants to merge 2 commits into
iNavFlight:maintenance-10.xfrom
MrScothh:fix/cli-tasks-average-rate

Conversation

@MrScothh

@MrScothh MrScothh commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

tasks prints a task's rate as 1 / its latest interval, and its load as the average execution time times that rate. A task that reschedules itself runs at uneven intervals, so a single interval says little: with #12063 the BARO task on a BMP280 runs a few times per measurement cycle (a forced-mode write, then short polls while the read is in flight), and twelve samples of tasks on a Holybro Kakute H7 Mini read 43 Hz and 0.6 % five times and 1934 to 2000 Hz and 5.5 % seven times, for the same steady behaviour. The total load at the bottom of the table jumps with it.

What I changed

The scheduler keeps a moving sum of each task's interval over 32 runs, next to the moving sum of its execution time it already keeps, and tasks computes the rate from that average. getTaskDeltaTime() and everything the scheduler decides on still use the latest interval. The sum is 32 bits and sits in the gap after taskLatestDeltaTime, so cfTask_t does not grow; cfTaskInfo_t.latestDeltaTime, read only by tasks, becomes averageDeltaTime.

Testing

Holybro Kakute H7 Mini, BMP280, #12063 merged in, twelve samples of tasks 100 s after boot: BARO reads 64 to 65 Hz and 0.6 % in all twelve (before: 43 or about 2000 Hz, as above). Other tasks read as before (PID about 1995 Hz, GYRO 3184 Hz, GPS 49 Hz, SYSTEM 9 Hz).

A task's first run after it is enabled, at boot or later, measures the time since it last ran, which can be minutes. That run starts the average at the task's desired period instead, and the average reaches the measured interval within about 100 runs (10 s for a 10 Hz task). On the same Kakute without #12063, with the barometer set to a sensor it does not have and turned on through HITL 181 s after boot (readMspSimulatorValues() enables TASK_BARO), the BARO row read 0, 1, 5, 16 and 19 Hz at 1, 3, 6, 10 and 20 s before this, and 20 Hz at 1 s and 19 Hz from 3 s on with it. Seven seconds after boot, SYSTEM, RX, BATTERY and GPS read 7, 44, 34 and 34 Hz before, and 10, 49, 49 and 49 Hz with it.

Cost against maintenance-10.x: RAM unchanged on MATEKF405, MATEKF722SE, KAKUTEF7, KAKUTEF7HDV, KAKUTEH7MINI and the H743; flash +40 to +290 B; the update runs in scheduler(), which grows by 28 B. ITCM grows by 24 B on MATEKF722SE and KAKUTEH7MINI, and by 64 B on KAKUTEF7 and KAKUTEF7HDV (16232 to 16296 of 16384 B), 36 of them in gyroFilter(), which LTO lays out differently. With #12063 also merged, KAKUTEF7HDV goes 32 B past its ITCM unless #12118, which takes afatfs_poll() out of scheduler(), is merged first; with all three it links at 15 448 B.

@qodo-code-review

Copy link
Copy Markdown
Contributor

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Show averaged task rates and loads in the CLI

🐞 Bug fix 🕐 20-40 Minutes

Grey Divider

AI Description

• Smooth task intervals so self-rescheduling tasks show a representative rate in tasks.
• Calculate per-task and total load from the averaged rate instead of the latest interval.
• Preserve latest-interval behavior for scheduler decisions and getTaskDeltaTime().
Diagram

graph TD
  A["Scheduler run"] --> B["Latest interval"] --> C["Interval sum"] --> D["Task info"] --> E["CLI tasks"]
  B --> F["Delta accessor"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Track a fixed window of intervals
  • ➕ Produces an exact average over the most recent runs.
  • ➖ Requires per-task sample storage or more complex window accounting, increasing memory and scheduler work.

Recommendation: Keep the PR's smoothed-sum approach. It addresses unstable CLI readings using the existing execution-time averaging pattern, without changing scheduling behavior or increasing the task structure's size. An exact window offers little benefit for this diagnostic display.

Files changed (3) +6 / -3

Bug fix (3) +6 / -3
cli.cCalculate displayed task rates from averaged intervals +2/-1

Calculate displayed task rates from averaged intervals

• The 'tasks' command uses 'averageDeltaTime' rather than the latest interval to calculate frequency. Its per-task and total load figures consequently use the smoothed rate.

src/main/fc/cli.c

scheduler.cAccumulate task intervals and expose their average +2/-1

Accumulate task intervals and expose their average

• Each selected task run updates a smoothed interval sum alongside its latest interval. 'getTaskInfo()' returns the sum divided by the smoothing count; 'getTaskDeltaTime()' remains unchanged.

src/main/scheduler/scheduler.c

scheduler.hAdd interval smoothing storage to task statistics +2/-1

Add interval smoothing storage to task statistics

• Adds a 32-bit interval sum to 'cfTask_t' in the space before its 64-bit statistics fields. Replaces the task-info structure's 'latestDeltaTime' field with 'averageDeltaTime'.

src/main/scheduler/scheduler.h

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Late-enabled tasks show zero rate ✓ Resolved
Description
scheduler() adds the entire time since lastExecutedAt to movingSumDeltaTime on a task’s first
run, and cliTasks() derives its rate from that sum. When a previously disabled task is enabled
more than about 134 seconds after boot, that one interval makes its reported average exceed one
second, so tasks shows 0 Hz for multiple subsequent executions even though the task is running.
Code

src/main/scheduler/scheduler.c[327]

+        selectedTask->movingSumDeltaTime += selectedTask->taskLatestDeltaTime - selectedTask->movingSumDeltaTime / TASK_MOVING_SUM_COUNT;
Evidence
setTaskEnabled() adds a task to the queue without updating lastExecutedAt; the scheduler then
accumulates the elapsed time since that timestamp and getTaskInfo() divides the sum by 32. The
simulator input path can enable TASK_BARO after its conditional startup enable. Once the
accumulated average exceeds 1,000,000 microseconds, the CLI’s integer rate calculation yields zero.

src/main/scheduler/scheduler.c[196-205]
src/main/scheduler/scheduler.c[324-329]
src/main/scheduler/scheduler.c[173-182]
src/main/fc/fc_tasks.c[414-420]
src/main/fc/fc_msp.c[4555-4562]
src/main/fc/cli.c[4411-4417]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A task enabled well after boot adds its entire disabled interval to the new moving sum, causing `tasks` to report 0 Hz while it runs.
## Fix Focus Areas
- src/main/scheduler/scheduler.c[196-205]
- src/main/scheduler/scheduler.c[324-329]
- src/main/fc/cli.c[4414-4415]
## Recommended Fix
Exclude the disabled interval when collecting the task’s first execution delta after enabling it. Preserve the latest interval used by scheduling, and add coverage for a task enabled after several minutes.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can route each severity your way: inline, summary, both, or drop

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

The first run after a task is enabled measures the time since it last ran,
so a task enabled minutes after boot showed 0 Hz in tasks for many runs.
That run now starts the average at the task's desired period instead.
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

RAM / Flash usage vs. base commit e87050f — commit b234944

Using the nearest available size baseline — the PR's exact base commit has no stored baseline yet.

Target Flash Δ RAM Δ
MATEKF405 +256 B (+0.04%) CCM: ±0 B (±0.00%)
RAM: +8 B (+0.01%)
MATEKF722 +96 B (+0.02%) ITCM_RAM: +24 B (+0.19%)
RAM: -4 B (-0.00%)
TCM: ±0 B (±0.00%)
MATEKF765 +168 B (+0.02%) DTCM_RAM: ±0 B (±0.00%)
SRAM1: ±0 B (±0.00%)
MATEKH743 +96 B (+0.01%) D2_RAM: ±0 B (±0.00%)
DTCM_RAM: ±0 B (±0.00%)
ITCM_RAM: +32 B (+0.20%)
RAM: ±0 B (±0.00%)

See RAM/flash optimization guide for techniques to reduce usage.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Test firmware build ready — commit b234944

Download firmware for PR #12117

251 targets built. Find your board's .hex file by name on that page (e.g. MATEKF405SE.hex). Files are individually downloadable — no GitHub login required.

Development build for testing only. Use Full Chip Erase when flashing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant