Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution window time

Tags:

erlang

elixir

I've read an article in the book elixir in action about processes and scheduler and have some questions:

  • Each process get a small execution window, what is does mean?
  • Execution windows is approximately 2000 function calls?
  • What is a process implicitly yield execution?
like image 364
softshipper Avatar asked Mar 11 '16 21:03

softshipper


1 Answers

Let's say you have 10,000 Erlang/Elixir processes running. For simplicity, let's also say your computer only has a single process with a single core. The processor is only capable of doing one thing at a time, so only a single process is capable of being executed at any given moment.

Let's say one of these processes has a long running task. If the Erlang VM wasn't capable of interrupting the process, every single other process would have to wait until that process is done with its task. This doesn't scale well when you're trying to handle tens of thousands of requests.

Thankfully, the Erlang VM is not so naive. When a process spins up, it's given 2,000 reductions (function calls). Every time a function is called by the process, it's reduction count goes down by 1. Once its reduction count hits zero, the process is interrupted (it implicitly yields execution), and it has to wait its turn.

Because Erlang/Elixir don't have loops, iterating over a large data structure must be done recursively. This means that unlike most languages where loops become system bottlenecks, each iteration uses up one of the process' reductions, and the process cannot hog execution.

The rest of this answer is beyond the scope of the question, but included for completeness.

Let's say now that you have a processor with 4 cores. Instead of only having 1 scheduler, the VM will start up with 4 schedulers (1 for each core). If you have enough processes running that the first scheduler can't handle the load in a reasonable amount of time, the second scheduler will take control of the excess processes, executing them in parallel to the first scheduler.

If those two schedulers can't handle the load in a reasonable amount of time, the third scheduler will take on some of the load. This continues until all of the processors are fully utilized.

Additionally, the VM is smart enough not to waste time on processes that are idle - i.e. just waiting for messages.

There is an excellent blog post by JLouis on How Erlang Does Scheduling. I recommend reading it.

like image 89
Cody Poll Avatar answered Nov 04 '22 02:11

Cody Poll