Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Erlang sleep (at night?)

I want to run a small clean up process every few hours on an Erlang server.

I know of the timer module. I saw an example in a tutorial used chained timer:sleep commands to wait for an event that would occur multiple days later, which I found strange. I understand that Erlang process are unique compared to those in other languages, but the idea of a process/thread sleeping for days, weeks, and even months at a time seemed odd.

So I set out to find out the details of what sleeping actually does. The closest I found was a blog post mentioning that sleep is implemented with a receive timeout, but that still left the question:

What do these sleep/sleep-like functions actually do?

Is my process taking up resources as it sleeps? Would having thousands of sleeping process use as many resources, as say, thousands of process servicing a recursive call that did nothing? Is there any performance penalty from repeatedly sleeping within processes, or sleeping for long periods of time? Is the VM constantly expending resources to see if the conditions to end the processes' sleep are up?

And as a side note, I'd appreciate if someone could comment on if there is a better way than sleeping to pause for hours or days at a time?

like image 591
Selali Adobor Avatar asked Sep 30 '22 04:09

Selali Adobor


2 Answers

That is the Karma of any erlang process: it waits or dies :o)

when a process is spawned, it start executing until the last execution line, and die, returning the last evaluation.

To keep a process alive, there is no other solution to recursively loop in a never ending succession of calls.

of course there are several conditions that make it stop or sleep:

  • end of the loop: the process received a message which tell him to stop recursion
  • a receive bloc: the process will wait until a message matching one entry in the receive bloc is posted in the message queue.
  • The VM scheduler stop it temporarily to let access to the CPU to other processes

in the 2 last cases the execution will restart under the responsibility of the VM scheduler.

while waiting it uses no CPU bandwidth, but keeps the exact same memory layout it had when it started waiting. The Erlang OTP offers some means to reduce this memory layout to the minimum using the hibernate option (see the documentation of gen_serevr or gen_fsm, but it is for advanced usage only in my mind).

a simple way to create a "signal" that will fire a process at regular (or almost regular) interval is effectively to use receive block with timout (The timeout is limited to 65535 ms), for example:

on_tick_sec(Module,Function,Arglist,Period) -> 
    on_tick(Module,Function,Arglist,1000,Period,0).
on_tick_mn(Module,Function,Arglist,Period) -> 
    on_tick(Module,Function,Arglist,60000,Period,0).
on_tick_hr(Module,Function,Arglist,Period) -> 
    on_tick(Module,Function,Arglist,60000,Period*60,0).



on_tick(Module,Function,Arglist,TimeBase,Period,Period) ->
    apply(Module,Function,Arglist),
    on_tick(Module,Function,Arglist,TimeBase,Period,0);
on_tick(Module,Function,Arglist,TimeBase,Period,CountTimeBase) ->
    receive
        stop -> stopped
    after TimeBase ->
        on_tick(Module,Function,Arglist,TimeBase,Period,CountTimeBase+1)
    end.

and usage:

1> Pid = spawn(util,on_tick_sec,[io,format,["hello~n"],5]).
<0.40.0>
hello                
hello                
hello                
hello                        
2> Pid ! stop.
stop
3>

[edit]

The timer module is a standard gen_server running in a separate process. All the function in the timer module are public interfaces that execute a hidden gen_server:call or gen_server:cast to the timer server. This is a common usage to hide the internal of a server and allow further evolutions without impact on existing applications.

The server uses internally a table (ets) to store all the actions it has to do along with each timer reference and it uses its own function to be awaken when needed (at the end, the VM must take care of this ?).

So you can hibernate a process without any effect on the timer server behavior. The hibernation mechanism is

  • tricky, see documentation at hibernate/3 definition, you will see that yo have to "rebuild" the context by yourself since everything was removed from the process context, and a tuple(Module,Function,Arguments} is stored by the system to restart your process when needed.
  • cost some time in garbage collecting and process restart

It is why I said that it is really an advance feature that need good reason to be used.

like image 179
Pascal Avatar answered Oct 31 '22 19:10

Pascal


There is also erlang:hibernate/3 that puts a process in "deep sleep", minimizing memory usage for it.

like image 38
Vlad Dumitrescu Avatar answered Oct 31 '22 20:10

Vlad Dumitrescu