lucidstack / ex-portmidi

Elixir bindings to the portmidi library
MIT License
35 stars 12 forks source link

One CPU Core Pinned at 100%? #8

Open djthread opened 7 years ago

djthread commented 7 years ago

Much thanks for sharing this library! It was great to find it as I set about trying to read some buttons on my controller. What I noticed, though, is that as soon as I run this line in iex

iex(1)> {:ok, input} = PortMidi.open(:input, "Midi Fighter 3D MIDI 1")
{:ok, #PID<0.177.0>}

... I get one of my cores pinned to 100%

It does seem to work alright otherwise, but something is clearly wrong. And thoughts on what it might be?

defmodule Test do
  @dev_name "Midi Fighter 3D MIDI 1"

  def start do
    {:ok, input} = PortMidi.open(:input, @dev_name)
    :ok = PortMidi.listen(input, self)
    go
  end

  def go do
    receive do
      msg ->
        IO.inspect msg
        go
    end
  end
end

Launching this and moving some controls does give me the messages back, but my CPU is working awfully hard for it.

iex(1)> Test.start
{#PID<0.177.0>, [{{146, 48, 127}, 4417}]}
{#PID<0.177.0>, [{{130, 48, 127}, 4534}]}

This is on an Arch Linux system.

lucidstack commented 7 years ago

Hey there, thank you for using ex-portmidi! Sorry about this (and for taking long to answer). It looks like we're gonna need a sleep call in portmidi's poll loop, to give some CPU time to the system.

I'll give this a try and let you know as soon as you can pull a new version! 🙇

djthread commented 7 years ago

Thank you for the response, @lucidstack! I'm glad you can corroborate my finding. I wonder if there might be some way to do a select on the socket somehow and avoid putting a sleep in the loop. I have no idea of the challenges in portmidi nor interfacing with it, but it's my understanding that putting a sleep in the main loop not really the best option.

For my little toy project here, I'm looking to map buttons on my Midi Fighter 3D to control my home server / living room. Being another process on my low power, multi-purpose server, I'd be concerned with adding unnecessary cpu work...

At any rate, I really appreciate your working on and sharing this library. It was really great to find.