[erlang-patches] [patch] fix excessive CPU consumption of timer_server

Björn Gustavsson bgustavsson@REDACTED
Thu Apr 11 17:12:37 CEST 2013


On Tue, Apr 9, 2013 at 9:27 PM, Aliaksey Kandratsenka <
alkondratenko@REDACTED> wrote:

>
> I've updated patch to use erlang:now instead of os:timestamp.
>
>
Thanks! We have now reviewed the code and have
found a minor potential issue.

The thing is that if two timers are set to the same
value, they are now guaranteed to expire in the same
order that they were set. That is because erlang:now/0
returns unique values. (See timer_timeout/1.)

With your patch, timers set to the same timeout
within the same millisecond will have identical
target times. Since the key in the ets table
(at least for one shot timers) is tuple with the
second element a reference, the ordering of the
refs will determine which key that erts:first/1 will
return if the target times are identical.

In the current implementation, it *happens* to
be true that if only and if Ref1 < Ref2, then
Ref1 was created before Ref2. But that property
is not guaranteed by the language definition and it
may be changed in a future release (e.g. to reduce
contention of the lock that is taken when refs are
created).

Therefore, I suggest fixing the bug by keeping
the original system_time/0 function and
round up when dividing by 1000. (The easiest
way to round up is to add 999 before dividing
by 1000.)


-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-patches/attachments/20130411/44b0d30d/attachment.htm>


More information about the erlang-patches mailing list