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

Task/semaphore tweaking #276

Merged
merged 20 commits into from
Oct 11, 2023
Merged

Task/semaphore tweaking #276

merged 20 commits into from
Oct 11, 2023

Conversation

bugadani
Copy link
Contributor

@bugadani bugadani commented Oct 10, 2023

get_systimer_count() is an AtomicU64 which is implemented using a critical section. The compiler is not smart enough to remove nested critical sections, so they mostly just waste time. If the slightly closer deadlines (caused by hoisting the timestamp reading out of the loop) can be tolerated, this PR should reduce instruction counts slightly in every loop iteration.

WORKER_HIGH doesn't need to be in an Option. It can be static-initialized instead.

TIMERS are only accessed in critical sections. I don't believe memory fencing is necessary between accessing individual array elements, though I admit I'm not quite sure what a fence achieves at all here.

All in all, this PR results in about 1kB reduction in firmware size for me.

@bugadani bugadani force-pushed the timer branch 2 times, most recently from dacddb1 to 676daa7 Compare October 10, 2023 18:59
@bugadani bugadani force-pushed the timer branch 2 times, most recently from d173733 to 9662b30 Compare October 10, 2023 19:32
@bugadani bugadani marked this pull request as ready for review October 10, 2023 19:49
@bugadani bugadani changed the title Task tweaking Task/semaphore tweaking Oct 10, 2023
@bugadani bugadani force-pushed the timer branch 4 times, most recently from f808505 to 66023c6 Compare October 10, 2023 20:53
@bugadani bugadani marked this pull request as draft October 10, 2023 20:55
@bugadani bugadani marked this pull request as ready for review October 10, 2023 21:06
@bjoernQ
Copy link
Contributor

bjoernQ commented Oct 11, 2023

From a brief look it looks good. The whole compat thing isn't exactly pretty but most of the weirdness there originates from problems with the Xtensa backend back then - I think the compiler got much better so it's great to revisit. Back then moving a static variable to a different file made the difference between working and crashing so I was very afraid of touching that code 😆

IMHO we shouldn't check-in the launch config

I'll have another look and run the smoke tests

@bugadani
Copy link
Contributor Author

IMHO we shouldn't check-in the launch config

Ooops, removed.

I have more changes in mind but I didn't want this PR to grow colossal.

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

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

LGTM, just one small nit :)

esp-wifi/src/compat/common.rs Outdated Show resolved Hide resolved
@bugadani
Copy link
Contributor Author

LGTM, just one small nit :)

Good eyes!

@bjoernQ
Copy link
Contributor

bjoernQ commented Oct 11, 2023

Still doing the smoketesting but there is one small thing:

warning: unused import: `Timer`
 --> esp-wifi\src\tasks.rs:8:24
  |
8 |         timer_compat::{Timer, TIMERS},
  |                        ^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

GH doesn't let me comment on that line

@bjoernQ
Copy link
Contributor

bjoernQ commented Oct 11, 2023

Did the tests and it almost looks good. There is one thing which is a bit sus:

Running the static_ip (cargo run --example static_ip --release --features "wifi") example on ESP32-C6 quite often ends up in a very quick

Wait to get connected
Disconnected

In that case the Disconnected is printed almost at the same time as Wait to get connected

It sometimes works but often not. That is different for me on main.

❯ rustc --version
rustc 1.75.0-nightly (bf9a1c8a1 2023-10-08)

@bugadani bugadani mentioned this pull request Oct 11, 2023
@bugadani
Copy link
Contributor Author

I really need to source a dev board for each line, don't I? ...

Can one of you guys help me bisect this issue? It seems very weird to me that a particular line would break. I would expect all RISC-V, but I am also not familiar with them.

@bugadani
Copy link
Contributor Author

One more thing:

Looking at old comments (#235 (review)) it smells to me if something else might be the culprit with static_ip. This isn't the first time you saw problem with that particular example and a RISC-V SoC and I have to wonder if this is really a bug with my changes. I'm not saying it's not, but it's suspicious :)

@bjoernQ
Copy link
Contributor

bjoernQ commented Oct 11, 2023

I'm also surprised it's only on one of the RISC-V socs ... I'll do a git bisect

@bjoernQ
Copy link
Contributor

bjoernQ commented Oct 11, 2023

Bisect got me to the first commit but maybe we should make sure it's not a red herring and have someone else verify this.

@MabezDev or @jessebraham - could you try to run the static_ip example 10 times on main and on this branch each and see if on this branch there are more unsuccessful connection attempts?

@bugadani
Copy link
Contributor Author

It's not impossible that the first commit has an impact here, as it's changing timer expiration times by a few cycles at least. I'd be very surprised for this to matter.

On the other hand, it turns out that on Xtensa, the timestamps must not be read in a critical section, as due to a race condition the function may occasionally return lower timestamps than expected. So if it matters where and how the code accesses the current time, it's a bit of a conflict between the two architectures.

@MabezDev
Copy link
Member

I see the same behaviour in this branch as the current main branch, so I don't think these changes have worsened anything.

@bjoernQ
Copy link
Contributor

bjoernQ commented Oct 11, 2023

I see the same behaviour in this branch as the current main branch, so I don't think these changes have worsened anything.

Thanks for checking this ..... then I guess I will merge it 👍

@bjoernQ bjoernQ merged commit 5a0e1d4 into esp-rs:main Oct 11, 2023
@bugadani bugadani deleted the timer branch October 11, 2023 14:14
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.

3 participants