Hacker News new | ask | show | jobs
by Jtsummers 20 days ago
> Erlang runtime semantics (cooperative "async threads")

Preemptive, not cooperative. Cooperative scheduling is based on the programmer releasing control manually (much like manual memory management) or having yield points baked into the language, often at function calls or IO events. Processes on BEAM can be preempted, they're allotted a number of "reductions" and when those are exhausted, the process is preempted (the process may also yield earlier). This guarantees every process is able to get a turn. Cooperative is fine, but it requires much more care. Go prior to 1.14 suffered from cooperative scheduling, a tight loop (not that you would write this one specifically, but as an example) could hog an OS thread:

  i := 0
  for {
    i++
  }
The conceptual equivalent in Erlang would only execute the loop so many times before it would be preempted.
2 comments

> Cooperative scheduling is based on the programmer releasing control manually (much like manual memory management) or having yield points baked into the language, often at function calls or IO events. Processes on BEAM can be preempted, they're allotted a number of "reductions" and when those are exhausted, the process is preempted (the process may also yield earlier).

BEAM is actually cooperative [1]. In userspace, function calls and IO (receive and !) are yield points, and reduction count is simply the number of function calls you're allowed. Something something Computer Sciencey reduction basically just means function call.

It just feels preemptive because as a functional language, you have to call functions all the time. Your loop becomes

   loop(I) -> loop(I + 1).
And that will yield once it calls itself enough times.

Native code (NIFs, BIFs, and drivers) has to count reductions and/or yield explicitly.

You can easily hog scheduler threads in native code that does significant computation or does i/o without using the runtime facilities; most people who write NIFs will mess it up at least once, I certainly have. It's much harder to do in userspace code, but if you fully unroll a very large loop that does not call functions or anything else that can yield, you can do it; I don't see how this would happen without specific intent, I've never run into it, but it's possible.

A blocked scheduler can be vaguely fine or terrible depending on how long it stays blocked and what the system is up to. Often it won't be terrible until you do something that needs all schedulers to synchronize, such as operations around code loading, but you can probably wedge the whole BEAM if you try.

[1] there's not really any other sane way to build preemptive userland threading anyway. You could schedule a timer signal, and task switch in the timer signal handler... but no sane person will recommend doing non-trivial work in a signal handler.

Show me how you'd unroll a loop which doesn't call functions in userspace.
I don't have a great understanding of the beam opcodes, but something like this:

    derp() ->
        A1 = get(a1),
        A2 = get(a2),
        A3 = get(a3),
        A4 = get(a4),
        A1 + A2 + A3 + A4.
But go out to I dunno a million (you might want to raise your atom limit) and I think it'll block a scheduler for a while. Beam Assembly for that is:

    {function, derp, 0, 2}.
      {label,1}.
        {line,[{location,"derp.erl",6}]}.
        {func_info,{atom,derp},{atom,derp},0}.
      {label,2}.
        {bif,get,{f,0},[{atom,a1}],{x,0}}.
        {bif,get,{f,0},[{atom,a2}],{x,1}}.
        {bif,get,{f,0},[{atom,a3}],{x,2}}.
        {bif,get,{f,0},[{atom,a4}],{x,3}}.
        {line,[{location,"derp.erl",11}]}.
        {gc_bif,'+',{f,0},4,[{x,0},{x,1}],{x,0}}.
        {gc_bif,'+',{f,0},4,[{tr,{x,0},{t_number,any}},{x,2}],{x,0}}.
        {gc_bif,'+',{f,0},4,[{tr,{x,0},{t_number,any}},{x,3}],{x,0}}.
        return.
I'm not 100%, but i don't think think get(...) is charged any reductions, so it won't yield. gc_bif potentially does garbage collection which does get charged reductions and BEAM can yield during gc.

I'm just adding at the end to do something with the values, so the compiler doesn't ignore them. If you wanted to write BEAM assembly, you can probably make a terrible function there without nearly as much work. smallnum math doesn't usually charge reductions from what I was seeing, so you could probably do something almost useful with that.

Right, my point is that C10k solutions basically revolve around M:N threading models where M being OS threads and N userspace threads, so from the view of the OS/Userspace boundary it's cooperative since the OS does nothing for the N userspace threads, only the M.

That the Erlang runtime hides it from the developer, doesn't mean that it isn't cooperative. So yes, for the Erlang programmer it's preemtive but from an OS/C standpoint it's cooperative (Like toast0 mentions in his sibling comment about NIF's exposing the cooperative nature of the runtime to programmers is telling about why it's considered "complicated" and not recommended practice).