Understanding resource usage when running external scripts

Hi, bit a noob with this sort of stuff.

I essentially want to know if/how i can get a full picture of resource usage while running an elixir script on the BEAM vm.

Specifically, I am running a node script via System.cmd and I want to know if i can measure the memory/cpu usage from the owner elixir process?

From my testing it seems that :erlang.memory(:total) does not account for memory consumed in my node script.


For clarity, here is my file. I run: iex> Runner.run()
In the JS I readFileSync of a 1MB file.

defmodule Mem do
  use GenServer

  defp schedule(), do: Process.send_after(self(), :check, 5)
  defp mem(), do: "#{:erlang.memory(:total) / :math.pow(1_024, 2)}"

  def start(), do: GenServer.start_link(__MODULE__, nil)

  def init(_) do
    schedule()
    {:ok, nil}
  end

  def handle_info(:check, _) do
    mem() |> IO.puts()

    schedule()
    {:noreply, nil}
  end
end

defmodule HeavyNodeScript do
  def run do
    Task.async_stream(1..10, fn _ ->
      res = System.cmd("node", ["#{File.cwd!()}/mem_check/consume.js"])
    end)
    |> Enum.to_list()
  end
end

defmodule Runner do
  def run do
    {:ok, pid} = Mem.start()
    HeavyNodeScript.run()
    Process.exit(pid, :kill)
  end
end

This will be very OS specific, so you will want to look into the primitives the OS supplies for reading such data about a process, then access that via Elixir. Like for linux it’s pretty easy, can just read the right data from the /proc database. :slight_smile:

1 Like