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

Implement simple allocator using enif_{alloc,free} #580

Merged
merged 1 commit into from
May 16, 2024
Merged

Conversation

filmor
Copy link
Member

@filmor filmor commented Dec 27, 2023

Implement a Rust global allocator using Erlang's enif_alloc and enif_free.

Rumor has it that this makes Rust's memory usage show up in erlang:memory().

The tests are currently reproducibly seg-faulting on Linux x86-64 but run through on arm64.

@filmor filmor force-pushed the rustler-alloc branch 2 times, most recently from 500cabc to 86bb26f Compare January 5, 2024 18:50
@filmor filmor force-pushed the rustler-alloc branch 3 times, most recently from 4a21c56 to 62aba90 Compare February 15, 2024 06:45
@filmor
Copy link
Member Author

filmor commented Feb 19, 2024

TODO: Check with valgrind what's going on

rustler/src/alloc.rs Outdated Show resolved Hide resolved
@dvic
Copy link
Contributor

dvic commented Mar 15, 2024

Locally on my Mac M1 both the Rust and Elixir tests are passing fine, so it might be something specific to the CI?

@filmor
Copy link
Member Author

filmor commented Mar 15, 2024

Some notes:

  • The error doesn't always happen on my machine (amd64, Linux). Currently, roughly every 5th full test suite execution segfaults at a random test.
  • The seed doesn't matter. Even with a seed that failed before, the next execution can go through.
  • I haven't had the time, yet, to run this with a debug Erlang instance.
  • This is not a blocker for new releases.

@filmor filmor force-pushed the rustler-alloc branch 3 times, most recently from 70f900a to 182f812 Compare May 2, 2024 19:51
@filmor
Copy link
Member Author

filmor commented May 2, 2024

Reading https://github.com/E-xyza/zigler/blob/main/priv/beam/allocator.zig brought the fix, this works now.

Copy link
Member

@evnu evnu left a comment

Choose a reason for hiding this comment

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

Cool that this works! :) I left some questions.

rustler/src/alloc.rs Outdated Show resolved Hide resolved
rustler/src/alloc.rs Show resolved Hide resolved
Copy link
Member

@evnu evnu left a comment

Choose a reason for hiding this comment

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

Great work! :)

Copy link
Member

@philss philss left a comment

Choose a reason for hiding this comment

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

Awesome! 🚀

Since we can only expect a 64bit alignment from the Erlang allocator, we
have to ensure larger alignments by overallocating. The allocator for
this case behaves the same way as the zigler "large beam allocator", see
https://github.com/E-xyza/zigler/blob/main/priv/beam/allocator.zig.

If the alignment is greater than 8, we allocate enough memory to store
an additional pointer. The result of the initial allocation is then
written immediately before the aligned pointer, which is returned from
the allocator. When deallocating, we can retrieve the original pointer
and pass it on to `enif_free`.
@filmor
Copy link
Member Author

filmor commented May 16, 2024

@evnu As requested, added more comments to the allocator.

@filmor filmor merged commit 8cd55cd into master May 16, 2024
34 checks passed
@filmor filmor deleted the rustler-alloc branch May 16, 2024 12:05
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.

None yet

5 participants