[erlang-questions] erlang:restart_timer

Björn-Egil Dahlberg <>
Thu Nov 6 15:27:09 CET 2014


Hi!

Ah, a new BIF .. third time's a charm? =)

Rickard is currently reworking the timer implementation so it is less 
problematic. The current implementation has some issues with scalability 
when a system has a lot of timers. The upcoming implementation can 
conceptually be thought of as processes with asynchronous signals sent 
between them. This includes the timer wheel as process (conceptually). 
I'm just mentioning this because a bif such as erlang:restart_timer/2 
would be affected by this rewrite.

In the current timer implementation it is trivial to implement 
restart_timer/2 and we see the function as a beneficial addition to the 
existing timer API. Rickard promised to fix everything in the rewrite. =)

+1

// Björn-Egil

On 2014-11-05 14:50, Tony Rogvall wrote:
> Hi!
>
> >From time to time I implement protocols and services that need watch dogs and/or retransmit timers.
> I will normally implement this using a BIF timer:
>
> 	WdtTimer = erlang:start_timer(Time, self(), wdt)
>
> This will send a message {timeout,WdtTimer, wdt} after Time milliseconds.
> When using a watchdog the code will normally try to avoid that the wdt is ever sent.
> The idea is that if you get the wdt message you are to late and need to crash / restart or something.
> So after som executing some "watched" code you want to re-arm the watch dog (feed it)
> This is normally done by:
>
> 	erlang:cancel_timer(WdtTimer),
> 	NewWdtTimer = erlang:start_timer(Time, self(), wdt)
>
> If you match the {timeout,WdtTimer,wdt} message and ignore {timeout,OldWdtTimer,wdt} messages,
> you could skip the cancel_timer. But that will generate plenty of waste.
>
> Protocols using retransmit timeouts also use a scheme like the above.
>
> I propose a new BIF:
>
> 	erlang:restart_timer(Ref, Time) -> false | RemainingTime
>
> This will restart a time if it is active and return the time that remained when it was restarted.
> If restart_timer return false either the timer has already time out or the reference is not a timer.
>
> The benefit is a speedup with at least 2.5 times. comparing:
>
> 	erlang:restart_timer(Ref, Time)
> vs
> 	erlang:cancel_timer(Ref)
> 	Ref1 = erlang:start_timer(Time, self(), Message)
>
> The cost saving is that restart_timer preserve already stored messages. And also internal timer structures are
> preserved. Resulting in less overhead.
>
> The interface is simple and do not require a new reference every restart, this could also save the time
> and heap updating a server state.
>
> I have an implementation that perform well, and could probably be optimized even further.
>
> What about it?  I'd like to hear from people actually implementing protocols, not only from the "normal" responders.
>
>
> /Tony
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>



More information about the erlang-questions mailing list