[erlang-questions] Sending message at a specific and accurate time

vicbaz@REDACTED vicbaz@REDACTED
Sun Feb 21 23:29:12 CET 2016


Hello,

Try +K flag.

$ erl
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:2:2] 
[async-threads:10] [hipe] [kernel-poll:false]

Eshell V7.2.1 (abort with ^G)
1> c(t).
{ok,t}
2> t:test().
[5003,5006,5002,5003,5006,5002,5003,5006,5001,5005]

$ erl +K true
Erlang/OTP 18 [erts-7.2.1] [source] [64-bit] [smp:2:2] 
[async-threads:10] [hipe] [kernel-poll:true]

Eshell V7.2.1 (abort with ^G)
1> c(t).
{ok,t}
2> t:test().
[5001,5001,5001,5001,5001,5001,5001,5001,5001,5001]

$ uname -a
Linux frontier 4.3.0-0.bpo.1-amd64 #1 SMP Debian 4.3.3-7~bpo8+1 
(2016-01-19) x86_64 GNU/Linux

$ cat /proc/cpuinfo | grep 'model name'
model name : Pentium(R) Dual-Core CPU E5300 @ 2.60GHz
model name : Pentium(R) Dual-Core CPU E5300 @ 2.60GHz

On 22/02/16 00:20, Joe Armstrong wrote:
> I tried a simpler program:
>
> test() -> test(10, []).
>
> test(0, L) ->
>      L;
> test(K, L) ->
>      T1 = ms_time(),
>      erlang:send_after(5000, self(), ping),
>      receive
>        ping ->
>           T2 = ms_time(),
>            test(K-1, [T2-T1|L])
>      end.
>
> ms_time() ->
>      erlang:system_time() div 1000000.
>
> Running this gives the following times
>
> [5001,5001,5006,5006,5002,5003,5006,5002,5002,5006]
>
> I'd expected 5000 or 5001
>
> This is in an unloaded OS with an unloaded erlang. 6ms seems very long -
> there are very few processes running and the system load is virtually zero.
>
> I tried erl -snp disable and setting process_flag(priority, max) but the results
> are pretty much the same.
>
> Waiting for shorter times like 100 ms makes no difference - still
> events with a 6 ms delay.
>
> I want to use this for scheduling music events (controlling synths) and these
> delays are far more than I expected.
>
> /Joe
>
>
>
> On Sun, Feb 21, 2016 at 8:32 PM, Jesper Louis Andersen
> <jesper.louis.andersen@REDACTED> wrote:
>>
>> On Sun, Feb 21, 2016 at 7:53 PM, Joe Armstrong <erlang@REDACTED> wrote:
>>>
>>> I'm getting about a 4 - 9 ms. inaccuracy in the time the messages is sent
>>> and
>>> the time I want it to be sent - but I'd like it to be much more accurate
>>> (sub ms if possible)
>>
>>
>> I would start by making measurements without the network component. When you
>> would send the gen_udp message, you take a timestamp, and analyze the skew
>> from the suggested skew. This lets you estimate the overhead of the rest of
>> the system in isolation from your own code and the Erlang VM.
>>
>> Intuitively, 4 to 9 milliseconds is much higher than what I would expect.
>> But note that if you sleep for, say, 40ms, you will be awoken on the 41ms
>> flank at the earliest. This is because you are usually "inside" a
>> millisecond when you make the call so you start by taking the ceiling of
>> that milli-second before you.
>>
>> How much other work is your Erlang VM doing when you make these
>> measurements? You are saying between 4 to 9 ms, which is variance suggesting
>> the VM has lots of work to do at that moment. And of course such stuff will
>> affect the running time. You can switch priority of your processes up to
>> high, but this comes at the expense of other calculations if you can't
>> finish your work quickly in the process with high priority.
>>
>>
>> --
>> J.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list