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

Esp wifi/more dynamic allocations #2396

Merged
merged 41 commits into from
Oct 31, 2024

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Oct 23, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the Migration Guide.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

Prefer dynamic allocations over static allocations internally.

Testing

All examples still work

esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
esp-wifi/src/compat/common.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
@bjoernQ bjoernQ force-pushed the esp-wifi/more-dynamic-allocations branch from 7649f37 to 27741c3 Compare October 24, 2024 09:40
@bjoernQ bjoernQ marked this pull request as ready for review October 24, 2024 10:06
@bjoernQ bjoernQ linked an issue Oct 24, 2024 that may be closed by this pull request
Copy link
Contributor

@bugadani bugadani left a comment

Choose a reason for hiding this comment

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

This is certainly better than before, but I'm hoping we can get rid of the huge amount of mallocs eventually.

esp-wifi/src/ble/mod.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/mod.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/mod.rs Outdated Show resolved Hide resolved
esp-wifi/src/common_adapter/common_adapter_esp32.rs Outdated Show resolved Hide resolved
esp-wifi/src/compat/common.rs Outdated Show resolved Hide resolved
esp-wifi/src/compat/common.rs Outdated Show resolved Hide resolved
esp-wifi/src/compat/timer_compat.rs Outdated Show resolved Hide resolved
esp-wifi/src/compat/timer_compat.rs Outdated Show resolved Hide resolved
esp-wifi/src/esp_now/mod.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/mod.rs Outdated Show resolved Hide resolved
esp-wifi/src/compat/common.rs Outdated Show resolved Hide resolved
@bjoernQ
Copy link
Contributor Author

bjoernQ commented Oct 25, 2024

This is certainly better than before, but I'm hoping we can get rid of the huge amount of mallocs eventually.

my thinking was that at least for the things indirectly allocated from the driver via malloc we currently allocate from internal-ram - we don't have control over that with the global allocator

t.b.h. I'm not sure if that would cause problems or not but if it's causing problems those things might be hard to debug

For things only allocated by our code I was fine using the global allocator (since I think these shouldn't cause problems)

@bugadani
Copy link
Contributor

my thinking was that at least for the things indirectly allocated from the driver via malloc we currently allocate from internal-ram - we don't have control over that with the global allocator

This would be a perfect use-case for allocator-api2, that way we could define an InternalAllocator and use that instead of mallocing.

@bjoernQ bjoernQ force-pushed the esp-wifi/more-dynamic-allocations branch from 7bfbea8 to d62c430 Compare October 28, 2024 13:36
item_size,
current_read: 0,
current_write: 0,
storage,
}
}

fn free_storage(&mut self) {
unsafe fn release_storage(&mut self) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I appreciate this function not causing a use-after-free, but I'd appreciate at least a note that this must be called before/during dropping the queue and that the struct should probably not be used after.

Can we move this into a Drop implementation and use ptr::drop_in_place instead of calling this manually?

@ProfFan
Copy link
Contributor

ProfFan commented Oct 28, 2024

my thinking was that at least for the things indirectly allocated from the driver via malloc we currently allocate from internal-ram - we don't have control over that with the global allocator

This would be a perfect use-case for allocator-api2, that way we could define an InternalAllocator and use that instead of mallocing.

I agree, I also saw some (de)allocations happening here within critical-sections, theoretically these should ideally happen through a bounded-time allocator instead of the global allocator (if the global allocator is not bounded time). Have you considered also using a lock-free queue instead?

esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
@bjoernQ
Copy link
Contributor Author

bjoernQ commented Oct 29, 2024

I'm not sure if I should change Vec into VecDequeue for the queues (i.e. FIFO vs LIFO)

@bjoernQ bjoernQ marked this pull request as draft October 30, 2024 11:48
@bjoernQ
Copy link
Contributor Author

bjoernQ commented Oct 30, 2024

Back to draft after observing this: #2274 (comment)

@bjoernQ bjoernQ marked this pull request as ready for review October 30, 2024 12:11
@bjoernQ
Copy link
Contributor Author

bjoernQ commented Oct 30, 2024

False alarm caused by unintendedly enabling ps-max-modem

@bjoernQ bjoernQ force-pushed the esp-wifi/more-dynamic-allocations branch from 0bd27f4 to 928b637 Compare October 30, 2024 12:25
Copy link
Contributor

@bugadani bugadani left a comment

Choose a reason for hiding this comment

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

I stopped after about half of the changeset. I have two pain points of different severity:

  • core::assert will include more strings than needed
  • there are a few addr_of! uses that look incorrect and I'd like you to double check them. Maybe it's just github reviews being clunky, but I have low confidence in them.

esp-wifi/MIGRATING-0.10.md Outdated Show resolved Hide resolved
esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/btdm.rs Outdated Show resolved Hide resolved
esp-wifi/src/ble/mod.rs Outdated Show resolved Hide resolved
if (*callout).dummy == 0 {
panic!("Trying to stop an uninitialzed callout");
}
core::assert!((*callout).dummy != 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason these aren't the assert from fmt.rs? I'd prefer using defmt if it's enabled.


let queue = (*queue).dummy as *mut ConcurrentQueue;
let mut event = event as usize;
(*queue).enqueue(addr_of_mut!(event).cast());
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks wrong, this passes the address of the parameter (i.e. points to the stack), not the event pointer itself. Same for remove, unless I'm missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I see

enqueue and remove take a pointer to some data of length item_size and copy from that location into the queue.
The NPL stuff however wants us to enqueue and dequeue a pointer

(Hope that explanation makes sense - not sure how to phrase it differently)

Copy link
Contributor

@bugadani bugadani Oct 30, 2024

Choose a reason for hiding this comment

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

Ehm okay I don't like this pattern but I guess it's OK because RawQueue copies out of the pointer. I'd prefer if we revisited this later and turned the queue into something typed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes - RawQueue was initially done to satisfy what the WiFi driver expects from a queue - in an upcoming PR we should use something other than RawQueue here for NPL (and looking back I should have split things into separate more granular PRs)

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.

This is getting a bit hard to review, so I suggest we don't add more to the PR.

Overall this a nice improvement, and I'm glad we're not storing things like the timer and rng in statics, this will allow me to do some nice things in #2301.

esp-wifi/src/common_adapter/mod.rs Show resolved Hide resolved
esp-wifi/src/compat/common.rs Show resolved Hide resolved
@bjoernQ bjoernQ force-pushed the esp-wifi/more-dynamic-allocations branch from c9c42e4 to 43c6151 Compare October 31, 2024 11:58
@MabezDev MabezDev added this pull request to the merge queue Oct 31, 2024
Merged via the queue into esp-rs:main with commit e5b923b Oct 31, 2024
28 checks passed
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.

esp-wifi: more static's in heap memory instead
4 participants