brandonpollack23

brandonpollack23

I was looking into lunatic which is a BEAM inspired framework for rust/wasm applications.

Seems pretty cool and it raised a concern I’ve had in the past about BEAM.

If I understand correctly (big if), their wasm runtime utilizes a timer to pre-empt their executing processes and switch between them.

This makes sense, their compilers dont necessarly insert reductions of any sort and WASM runtimes don’t do that either.

My understanding is this:

A timer has some downsides. Some of them include requiring a thread to monitor it and pre-empt other running OS Threads. This means you have to set some state that causes them to stop executing or send some manager of them a message to stop their execution–or use a re-entrant lock–and save the state/bookkeeping etc. This is how setTimeout etc work and it can be done with data structure of timers and a sleeping (interuptable thread).

I’ve also heard vaguely that timers are unreliable or slow, but I don’t quite understand how this is true on a single machine (since this timer would be on one thread).

Reductions are simpler and allow the VM to pre-empt itself somewhat instead of relying on a mechanism to do so.

Basically this is all floating in my head and I’d love to start a discourse on this and get it more out there so when others think this there is an answer why not timer based pre-emption and yes reductions for the use case of Erlang/Elixir, but fair justification on how a timer may work out for others (if it would). Has anyone ever investigated these alternatives? I am certain this has been thought about and tried but I can’t quite find info on it.

Showing Posts 1 to 4

jhogberg

jhogberg

Erlang Core Team

I experimented with this a long time ago, using a fixed-frequency timer that set a flag to schedule processes out whenever two ticks passed without being them scheduled out by other means (e.g. receive). It worked but it wasn’t faster than reduction counting, and back then there was no clear road to make that happen. The JIT may help us remove some overhead but I’m still not sure it would lead to any concrete gains.

dimitarvp

dimitarvp

I am not close to the implementation details at all but I’d think that timers are still more reliable than reductions; imagine a bad actor just spawning a number of processes (bigger than the schedulers count) that all just do stuff without recursing or calling any other function. Come to think of it, I never tried it – does the BEAM actually truly stall then? Probably not.

Though IMO a good compromise (if one doesn’t want to use timers) is that compilers should just insert the right code at the right place, kind of like Golang does.

From my experience with Rust so far, work-stealing schedulers are also an amazing way to do things.

jhogberg

jhogberg

Erlang Core Team

I am not close to the implementation details at all but I’d think that timers are still more reliable than reductions; imagine a bad actor just spawning a number of processes (bigger than the schedulers count) that all just do stuff without recursing or calling any other function. Come to think of it, I never tried it – does the BEAM actually truly stall then? Probably not.

If you purposefully call things that don’t count reductions or yield appropriately, e.g. badly written NIFs, then yes, it would stall.

There are ways to cause issues in Erlang code, but you would have to write some truly contrived code for a bad actor to exploit it. Should anyone run into such an issue with real code we’ll fix it then and there.

However, these things would misbehave just as badly if implemented with timers as there’s no (edit: sane, documented, and cross-platform) way for user-space to preempt something that isn’t prepared to be preempted. The best that a user-space implementation can do is to set a flag or whatever that is later checked, and if that never happens, the target will never be scheduled out (c.f. POSIX thread cancellation: nothing happens until a cancellation point is reached).

Though IMO a good compromise (if one doesn’t want to use timers) is that compilers should just insert the right code at the right place, kind of like Golang does.

This is more or less how our implementation works, too.

From my experience with Rust so far, work-stealing schedulers are also an amazing way to do things.

Our schedulers steal work from each other when appropriate.

dimitarvp

dimitarvp

Yep, I suspected. It’s one of the things that makes the BEAM so good. Keep at it. :heart:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews