Skip to content

fix(airtime): set secSinceBoot from millis() to address inaccurate airtime calculations - #9778

Closed
m1nl wants to merge 13 commits into
meshtastic:developfrom
m1nl:fix_airtime_calculations
Closed

fix(airtime): set secSinceBoot from millis() to address inaccurate airtime calculations#9778
m1nl wants to merge 13 commits into
meshtastic:developfrom
m1nl:fix_airtime_calculations

Conversation

@m1nl

@m1nl m1nl commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

The current airtime calculation relies on the OSThread being triggered every second. However, when the device enters sleep mode or other tasks take longer to execute, the airtime OSThread may be delayed, leading to inaccurate calculations.

I propose replacing the secSinceBoot timer, which is currently incremented inside the airtime OSThread, with the millis() function from the Arduino framework. The millis() function returns the number of milliseconds since the board began running the current program. According to the documentation, millis() provides a reliable uptime value regardless of whether the device is sleeping or temporarily busy with other tasks, making it a more robust basis for airtime calculations.

I've also decreased interval for airtime OSThread to 100ms - I consider this to be a safe value as most of maintenance tasks run with this interval. We don't need to make it equal to 1000ms, when millis() invocation provides accurate uptime.

  • I have tested that my proposed changes behave as described.
  • I have tested that my proposed changes do not cause any obvious regressions on the following devices:
    • Heltec (Lora32) V3
    • LilyGo T-Deck
    • LilyGo T-Beam
    • RAK WisBlock 4631
    • Seeed Studio T-1000E tracker card
    • Other (please specify below)

Previous implementation relied on OSThread being triggered every
second. When device is sleeping or other tasks take more time
airtime OSThread may become delayed and calculations will become
invalid.
@github-actions github-actions Bot added the bugfix Pull request that fixes bugs label Feb 28, 2026
@thebentern
thebentern requested review from GUVWAF and Copilot February 28, 2026 16:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the airtime/accounting timebase to avoid relying on a 1 Hz OSThread tick, aiming to make airtime/utilization tracking resilient to scheduling delays (sleep/long tasks).

Changes:

  • Replaces secSinceBoot (manually incremented) with millis()/1000 as the uptime source for airtime calculations.
  • Removes the secSinceBoot member and its increment in AirTime::runOnce().
  • Decreases the AirTime OSThread interval from 1000ms to 100ms.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/airtime.h Removes secSinceBoot state from the AirTime class.
src/airtime.cpp Switches getSecondsSinceBoot() to millis()/1000 and adjusts the airtime thread cadence.

Comment thread src/airtime.cpp Outdated
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp Outdated
@GUVWAF

GUVWAF commented Feb 28, 2026

Copy link
Copy Markdown
Member

In general this sounds good to me. I don't really like that it's running every 100ms, though. There are not a lot of things that are by default running at a certain interval, and in principle we could get rid of it here. We only need to update airtime when receiving or transmitting a packet, and at that time we could check in which "bin" we are with millis(). Not sure you think this is a reasonable change, but it would be really nice to do it like this.

@m1nl

m1nl commented Feb 28, 2026

Copy link
Copy Markdown
Contributor Author

In general this sounds good to me. I don't really like that it's running every 100ms, though. There are not a lot of things that are by default running at a certain interval, and in principle we could get rid of it here. We only need to update airtime when receiving or transmitting a packet, and at that time we could check in which "bin" we are with millis(). Not sure you think this is a reasonable change, but it would be really nice to do it like this.

I like this idea too! However that's way much complicated change than I proposed. If needed we can restore 1000ms interval, that should not impact airtime calculation accuracy.

I can try to redesign airtime calculation in line with idea you brought up in a separate PR.

Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
thebentern and others added 3 commits March 2, 2026 06:46
In case device sleeps and periods are missed, ensure the windows
which are between previous and current window are also cleared.
@m1nl

m1nl commented Mar 2, 2026

Copy link
Copy Markdown
Contributor Author

@robekl I applied changes from your comments, please review

@m1nl
m1nl requested a review from robekl March 2, 2026 20:32
@m1nl
m1nl force-pushed the fix_airtime_calculations branch from d8b0a6a to 15c9bd8 Compare March 2, 2026 20:33
This ensures period won't change during thread execution.
@m1nl
m1nl force-pushed the fix_airtime_calculations branch from 15c9bd8 to 9d0c6c9 Compare March 2, 2026 20:34
@m1nl m1nl changed the title fix(airtime): replace secSinceBoot by millis() to address inaccurate airtime calculations fix(airtime): set secSinceBoot from millis() to address inaccurate airtime calculations Mar 2, 2026
@thebentern
thebentern requested a review from Copilot March 3, 2026 02:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.

Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp
Comment thread src/airtime.cpp

uint32_t AirTime::getSecondsSinceBoot()
{
return this->secSinceBoot;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what the AI is referring to. Does it make sense to give the millis treatment here too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, from what I can see, Copilot seems to question almost every change just to raise concerns. Earlier, @robekl brought up a valid point that we should keep a stable value of secSinceBoot. Small individual drifts of less than a minute don’t matter much; at least we wouldn’t be accumulating the error, which is what’s happening right now.

The change I proposed initially seemed very small to me, but it looks like we might actually benefit from a full rewrite of the airtime calculation logic. I might try rewriting it from scratch, although I’m not sure how welcome those changes would be, since it touches some internals that haven’t been modified in a long time...

@github-actions github-actions Bot added the Stale Issues that will be closed if not triaged. label May 13, 2026
@github-actions github-actions Bot closed this May 30, 2026
@thebentern thebentern reopened this May 30, 2026
@github-actions github-actions Bot removed the Stale Issues that will be closed if not triaged. label Aug 1, 2026
@NomDeTom

Copy link
Copy Markdown
Collaborator

closed as #11291 has landed, and a different approach was taken. Many thanks and much of the credit is due to @m1nl for this contribution.

@NomDeTom NomDeTom closed this Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants