Nif and driver callbacks

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Nif and driver callbacks

Mikael Karlsson-7
Hi,
I just started to write some nif code to interface towards a Linux driver API.
I can install a callback function in my nif module that gets called from the driver  whenever it is ready to receive new data. Currently it gets called every 5 ms. The callback function sends a message to an erlang process that in turn delivers new data to the driver. (Using another nif function).

This works, although I am a bit puzzled how, since the callback function that is called from the linux driver appearantly is running outside the Erlang VM and still is able to send a message to the erlang process. Could anyone explain how this works?

Also when loaded with a lot of data I am not capable of delivering in time (5 ms) unless I set the process_flag(priority, high). I noticed there is a enif_select function that I can use instead but this also sends a message to an erlang process so I am not sure if there is any timing difference between using enif_select or sending an erlang message directly from a driver callback?

Thanks,
Mikael


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Nif and driver callbacks

Sverker Eriksson-5
On tor, 2019-05-23 at 15:54 +0200, Mikael Karlsson wrote:

> Hi,
> I just started to write some nif code to interface towards a Linux driver API.
> I can install a callback function in my nif module that gets called from the
> driver  whenever it is ready to receive new data. Currently it gets called
> every 5 ms. The callback function sends a message to an erlang process that in
> turn delivers new data to the driver. (Using another nif function).
>
> This works, although I am a bit puzzled how, since the callback function that
> is called from the linux driver appearantly is running outside the Erlang VM
> and still is able to send a message to the erlang process. Could anyone
> explain how this works?
>

If your callback function calls enif_send to send the message and the message
arrives, then apparently it's running within the Erlang VM.

If in doubt:
1. Enable core dump generation (ulimit -c, /proc/sys/kernel/core_pattern, etc).
2. Put a call to abort() in your callback.
3. Run and watch your beam.smp crash.
4. Do post mortem with "gdb beam.smp <core-dump>"
5. Use 'backtrace' command to see who is calling you.

/Sverker

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Nif and driver callbacks

Mikael Karlsson-7
In reply to this post by Mikael Karlsson-7
Thu May 23 20:42:37 CEST 2019 Sverker Ericsson wrote:

> On tor, 2019-05-23 at 15:54 +0200, Mikael Karlsson wrote:
>> Hi,
>> I just started to write some nif code to interface towards a Linux driver API.
>> I can install a callback function in my nif module that gets called from the
>> driver  whenever it is ready to receive new data. Currently it gets called
>> every 5 ms. The callback function sends a message to an erlang process that in
>> turn delivers new data to the driver. (Using another nif function).
>>
>> This works, although I am a bit puzzled how, since the callback function that
>> is called from the linux driver appearantly is running outside the Erlang VM
>> and still is able to send a message to the erlang process. Could anyone
>> explain how this works?
>>

> If your callback function calls enif_send to send the message and the message
> arrives, then apparently it's running within the Erlang VM.
>
> If in doubt:
> 1. Enable core dump generation (ulimit -c, /proc/sys/kernel/core_pattern, etc).
> 2. Put a call to abort() in your callback.
> 3. Run and watch your beam.smp crash.
> 4. Do post mortem with "gdb beam.smp <core-dump>"
> 5. Use 'backtrace' command to see who is calling you.

> /Sverker

Thanks,
I am using enif_send and I realise that it is running within the VM (beam.smp) process now.
And it works fine even though I am still wondering how the asynchronous notification plays together with the Erlang scheduling, or maybe it doesn't need to, but just works anyway.

Mikael


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions