Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pandad: refactor to consolidate threads, keep only one can_send thread #32680

Merged
merged 4 commits into from
Aug 7, 2024

Conversation

deanlee
Copy link
Contributor

@deanlee deanlee commented Jun 10, 2024

To minimize changes and avoid introducing new issues, the original C-style functions have been retained.

Main Changes

  1. Renamed xxx_thread() functions to xxx().
  2. Changed variables in the original thread functions to static to retain values across calls.
  3. Moved send_peripheral_state from panda_state() to peripheral_state() for a better fit.
  4. Move 'safety_future' to a global variable to ensure the safety_setter_thread gracefully quits on exit.
  5. Prefix all global variables with 'g_'

Additional Note

A possible better approach to eliminate the static variables is to declare classes for PeripheralState, PandaState, etc., and move the static variables to their member variables. The final code might look something like this:

void PandaD::run() {
    ...
    panda_state.process();
    peripheral_state.process();
}

However, since the pandad process always restarts after failures, keeping these variables as static in this PR is harmless.

Copy link
Contributor

github-actions bot commented Jun 10, 2024

Thanks for contributing to openpilot! In order for us to review your PR as quickly as possible, check the following:

  • Convert your PR to a draft unless it's ready to review
  • Read the contributing docs
  • Before marking as "ready for review", ensure:
    • the goal is clearly stated in the description
    • all the tests are passing
    • the change is something we merge
    • include a route or your device' dongle ID if relevant

@deanlee deanlee force-pushed the pandad_single_thread branch 6 times, most recently from f4f3cf3 to 71fe95a Compare June 10, 2024 16:53
@adeebshihadeh adeebshihadeh added this to the 0.9.8 milestone Jun 10, 2024
@deanlee deanlee force-pushed the pandad_single_thread branch 2 times, most recently from 21dc1ba to c00f778 Compare June 13, 2024 18:55
@deanlee deanlee marked this pull request as ready for review June 14, 2024 10:02
@adeebshihadeh
Copy link
Contributor

Nice work! I added to the 0.9.8 milestone and will try to get this merged soon.

@github-actions github-actions bot added the stale label Jun 30, 2024
@deanlee deanlee force-pushed the pandad_single_thread branch 2 times, most recently from abd0028 to 8a06e68 Compare June 30, 2024 08:34
@commaai commaai deleted a comment from github-actions bot Jun 30, 2024
@adeebshihadeh
Copy link
Contributor

trigger-jenkins

@deanlee deanlee marked this pull request as draft August 3, 2024 04:35
@deanlee deanlee marked this pull request as ready for review August 3, 2024 06:29
@deanlee deanlee changed the title pandad: refactor to single thread pandad: refactor to consolidate threads, keep only one can_send thread Aug 3, 2024
selfdrive/pandad/pandad.cc Outdated Show resolved Hide resolved
selfdrive/pandad/pandad.cc Outdated Show resolved Hide resolved

{
sm.update(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

sm.update should probably happen in the main pandad_run loop for readability

Copy link
Contributor Author

@deanlee deanlee Aug 4, 2024

Choose a reason for hiding this comment

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

There are two submasters updated at different frequencies. For now, keeping updates in their own functions provides better encapsulation. Moving to pandad_run would add complexity. We can address this in a future refactor or another PR.

@adeebshihadeh adeebshihadeh marked this pull request as draft August 3, 2024 22:05
@deanlee deanlee marked this pull request as ready for review August 4, 2024 14:03
#include "cereal/messaging/messaging.h"
#include "common/swaglog.h"

void PandaSafety::configureSafetyMode() {
Copy link
Contributor

Choose a reason for hiding this comment

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

this is cleaner than what we had before, but I think it'll be more readable as a single function like "safety_mode_tick"

@adeebshihadeh
Copy link
Contributor

@Quantizr can you verify this in the testing closet and merge if it looks good?

@Quantizr
Copy link
Contributor

Quantizr commented Aug 7, 2024

Looks good on the testing closet, significant reduction in the variability of the timing of pandaStates messages, going from a relative standard deviation of ~0.0175 to ~0.0049 and small improvements in various other service timings.
This PR timings:
testing comma life_revisions_6d2fed1745fb901f8b77d1086e27be8945e6c97d

Prior timings:
testing comma life_revisions_d00a53983089be6bed0e037474abfea823f2fc3e

@Quantizr Quantizr merged commit a4de873 into commaai:master Aug 7, 2024
17 checks passed
@deanlee deanlee deleted the pandad_single_thread branch September 13, 2024 16:20
Edison-CBS pushed a commit to Edison-CBS/openpilot that referenced this pull request Sep 15, 2024
commaai#32680)

* single thread

improve comment

* Keep can_send() running in a separate thread

* send send_peripheral_state in pandad_run

* new PandaSafety class
old-commit-hash: a4de873
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants