[erlang-questions] Calling (equivalent of) erlang:send_after/3 from a NIF?

Roger Lipscombe roger@REDACTED
Tue Oct 30 11:15:25 CET 2018


On 30 October 2018 at 09:37, Lukas Larsson <lukas@REDACTED> wrote:
> No, there is no way to do this.

Pity.

> One of the reasons this would be difficult
> to implement is because it is currently only possible to create timers on
> normal schedulers. So that functionality would have to be extended to be
> useable from dirty schedulers, which is no small task.

I'd be using it from a background thread -- cf enif_send(NULL, ...) --
 rather than a dirty scheduler, but I imagine the same constraint
applies?

> If I were to do what you are after, I would first see if it is fast enough
> to setup timers as the nif returns, using a monotonic timestamp to adjust
> them for the time it took for the nif to return.

Because this would potentially be from a background, arbitrary, thread
-- as in: there's no well-defined place to return this from -- that's
not gonna fly, unfortunately.

> Another idea would be to use timerfd_create and then do a enif_select on
> that.

That involves a kernel-mode transition, which -- since
Spectre/Meltdown -- is something we're trying to avoid, particularly
since we're on VMs. I might try it for comparison, though.

> Or just roll your own thread, with a small enough number of counters
> it should be simple enough.

We're potentially looking at ~20-25K timers (not just curl-required
ones). I was hoping to leverage someone else's timer wheel
implementation :)

Thanks,
Roger.



More information about the erlang-questions mailing list