diff --git a/lib/livebook/application.ex b/lib/livebook/application.ex index 41f87c7c3..5c7a2d614 100644 --- a/lib/livebook/application.ex +++ b/lib/livebook/application.ex @@ -16,6 +16,8 @@ defmodule Livebook.Application do LivebookWeb.Telemetry, # Start the PubSub system {Phoenix.PubSub, name: Livebook.PubSub}, + # Periodid measurement of system resources + Livebook.SystemResources, # Start the tracker server on this node {Livebook.Tracker, pubsub_server: Livebook.PubSub}, # Start the supervisor dynamically managing sessions diff --git a/lib/livebook/evaluator.ex b/lib/livebook/evaluator.ex index e8c4b6e0c..fab800151 100644 --- a/lib/livebook/evaluator.ex +++ b/lib/livebook/evaluator.ex @@ -68,6 +68,31 @@ defmodule Livebook.Evaluator do end end + @doc """ + Computes the memory usage from this evaluator node. + """ + @spec memory :: Livebook.Runtime.runtime_memory() + def memory do + %{ + total: total, + processes: processes, + atom: atom, + binary: binary, + code: code, + ets: ets + } = Map.new(:erlang.memory()) + + %{ + total: total, + processes: processes, + atom: atom, + binary: binary, + code: code, + ets: ets, + other: total - processes - atom - binary - code - ets + } + end + @doc """ Asynchronously parses and evaluates the given code. @@ -85,8 +110,6 @@ defmodule Livebook.Evaluator do * `:file` - file to which the evaluated code belongs. Most importantly, this has an impact on the value of `__DIR__`. - * `:notify_to` - a pid to be notified when an evaluation is finished. - The process should expect a `{:evaluation_finished, ref}` message. """ @spec evaluate_code(t(), pid(), String.t(), ref(), ref() | nil, keyword()) :: :ok def evaluate_code(evaluator, send_to, code, ref, prev_ref \\ nil, opts \\ []) when ref != nil do @@ -235,7 +258,6 @@ defmodule Livebook.Evaluator do file = Keyword.get(opts, :file, "nofile") context = put_in(context.env.file, file) start_time = System.monotonic_time() - notify_to = Keyword.get(opts, :notify_to) {result_context, response} = case eval(code, context.binding, context.env) do @@ -257,9 +279,8 @@ defmodule Livebook.Evaluator do Evaluator.IOProxy.clear_input_cache(state.io_proxy) output = state.formatter.format_response(response) - metadata = %{evaluation_time_ms: evaluation_time_ms} + metadata = %{evaluation_time_ms: evaluation_time_ms, memory_usage: memory()} send(send_to, {:evaluation_response, ref, output, metadata}) - if notify_to, do: send(notify_to, {:evaluation_finished, ref}) :erlang.garbage_collect(self()) {:noreply, state} diff --git a/lib/livebook/runtime.ex b/lib/livebook/runtime.ex index 0f8ad0a53..0a420861f 100644 --- a/lib/livebook/runtime.ex +++ b/lib/livebook/runtime.ex @@ -127,7 +127,6 @@ defprotocol Livebook.Runtime do ets: non_neg_integer(), other: non_neg_integer(), processes: non_neg_integer(), - system: non_neg_integer(), total: non_neg_integer() } @@ -174,7 +173,7 @@ defprotocol Livebook.Runtime do * `{:evaluation_response, ref, output, metadata}` - final result of the evaluation. Recognised metadata entries - are: `evaluation_time_ms` + are: `evaluation_time_ms` and `memory_usage` The output may include input fields. The evaluation may then request the current value of a previously rendered input by diff --git a/lib/livebook/runtime/erl_dist/runtime_server.ex b/lib/livebook/runtime/erl_dist/runtime_server.ex index 4f4034101..d97d18387 100644 --- a/lib/livebook/runtime/erl_dist/runtime_server.ex +++ b/lib/livebook/runtime/erl_dist/runtime_server.ex @@ -179,14 +179,8 @@ defmodule Livebook.Runtime.ErlDist.RuntimeServer do end end - def handle_info({:evaluation_finished, _ref}, state) do - send_memory_usage(state) - Process.cancel_timer(state.memory_timer_ref) - {:noreply, schedule_memory_usage(state)} - end - def handle_info(:memory_usage, state) do - send_memory_usage(state) + send(state.owner, {:memory_usage, Evaluator.memory()}) {:noreply, schedule_memory_usage(state)} end @@ -204,7 +198,6 @@ defmodule Livebook.Runtime.ErlDist.RuntimeServer do state ) do state = ensure_evaluator(state, container_ref) - opts = Keyword.put(opts, :notify_to, self()) prev_evaluation_ref = case prev_locator do @@ -316,17 +309,4 @@ defmodule Livebook.Runtime.ErlDist.RuntimeServer do ref = Process.send_after(self(), :memory_usage, @memory_usage_interval) %{state | memory_timer_ref: ref} end - - defp send_memory_usage(state) do - memory = - :erlang.memory() - |> Enum.into(%{}) - |> Map.drop([:processes_used, :atom_used]) - - other = - memory.total - memory.processes - memory.atom - memory.binary - memory.code - memory.ets - - memory = Map.put(memory, :other, other) - send(state.owner, {:memory_usage, memory}) - end end diff --git a/lib/livebook/session.ex b/lib/livebook/session.ex index 3ba3f9429..5ca73f452 100644 --- a/lib/livebook/session.ex +++ b/lib/livebook/session.ex @@ -55,8 +55,6 @@ defmodule Livebook.Session do alias Livebook.Users.User alias Livebook.Notebook.{Cell, Section} - @memory_usage_interval 15_000 - @type t :: %__MODULE__{ id: id(), pid: pid(), @@ -76,14 +74,13 @@ defmodule Livebook.Session do autosave_timer_ref: reference() | nil, save_task_pid: pid() | nil, saved_default_file: FileSystem.File.t() | nil, - system_memory_timer_ref: reference() | nil, memory_usage: memory_usage() } @type memory_usage :: %{ runtime: Livebook.Runtime.runtime_memory() | nil, - system: Livebook.Utils.system_memory() + system: Livebook.SystemResources.memory() } @typedoc """ @@ -458,7 +455,7 @@ defmodule Livebook.Session do do: dump_images(state, images), else: :ok ) do - state = state |> schedule_autosave() |> schedule_system_memory_update() + state = schedule_autosave(state) {:ok, state} else {:error, error} -> @@ -479,8 +476,7 @@ defmodule Livebook.Session do autosave_path: opts[:autosave_path], save_task_pid: nil, saved_default_file: nil, - system_memory_timer_ref: nil, - memory_usage: %{runtime: nil, system: Utils.fetch_system_memory()} + memory_usage: %{runtime: nil, system: Livebook.SystemResources.memory()} } {:ok, state} @@ -521,11 +517,6 @@ defmodule Livebook.Session do end end - defp schedule_system_memory_update(state) do - ref = Process.send_after(self(), :system_memory, @memory_usage_interval) - %{state | system_memory_timer_ref: ref} - end - @impl true def handle_call(:describe_self, _from, state) do {:reply, self_from_state(state), state} @@ -781,8 +772,14 @@ defmodule Livebook.Session do end def handle_info({:evaluation_response, cell_id, response, metadata}, state) do + {memory_usage, metadata} = Map.pop(metadata, :memory_usage) operation = {:add_cell_evaluation_response, self(), cell_id, response, metadata} - {:noreply, handle_operation(state, operation)} + + {:noreply, + state + |> put_memory_usage(memory_usage) + |> handle_operation(operation) + |> notify_update()} end def handle_info({:evaluation_input, cell_id, reply_to, input_id}, state) do @@ -832,17 +829,8 @@ defmodule Livebook.Session do {:noreply, handle_save_finished(state, result, file, default?)} end - def handle_info(:system_memory, state) do - {:noreply, state |> update_system_memory_usage() |> schedule_system_memory_update()} - end - def handle_info({:memory_usage, runtime_memory}, state) do - Process.cancel_timer(state.system_memory_timer_ref) - system_memory = Utils.fetch_system_memory() - memory = %{runtime: runtime_memory, system: system_memory} - state = %{state | memory_usage: memory} - notify_update(state) - {:noreply, state} + {:noreply, state |> put_memory_usage(runtime_memory) |> notify_update()} end def handle_info(_message, state), do: {:noreply, state} @@ -1010,16 +998,15 @@ defmodule Livebook.Session do defp after_operation(state, _prev_state, {:set_notebook_name, _pid, _name}) do notify_update(state) - state end defp after_operation(state, _prev_state, {:set_runtime, _pid, runtime}) do if runtime do state else - put_in(state.memory_usage.runtime, nil) - |> update_system_memory_usage() - |> schedule_system_memory_update() + state + |> put_memory_usage(nil) + |> notify_update() end end @@ -1040,8 +1027,6 @@ defmodule Livebook.Session do end notify_update(state) - - state end defp after_operation( @@ -1166,10 +1151,15 @@ defmodule Livebook.Session do Phoenix.PubSub.broadcast(Livebook.PubSub, "sessions:#{session_id}", message) end + defp put_memory_usage(state, runtime) do + put_in(state.memory_usage, %{runtime: runtime, system: Livebook.SystemResources.memory()}) + end + defp notify_update(state) do session = self_from_state(state) Livebook.Sessions.update_session(session) broadcast_message(state.session_id, {:session_updated, session}) + state end defp maybe_save_notebook_async(state) do @@ -1310,10 +1300,4 @@ defmodule Livebook.Session do defp container_ref_for_section(%{parent_id: nil}), do: :main_flow defp container_ref_for_section(section), do: section.id - - defp update_system_memory_usage(state) do - state = put_in(state.memory_usage.system, Utils.fetch_system_memory()) - notify_update(state) - state - end end diff --git a/lib/livebook/system_resources.ex b/lib/livebook/system_resources.ex new file mode 100644 index 000000000..6a7678dd9 --- /dev/null +++ b/lib/livebook/system_resources.ex @@ -0,0 +1,37 @@ +defmodule Livebook.SystemResources do + # Periodically compute system resource usage. + @moduledoc false + @type memory :: %{total: non_neg_integer(), free: non_neg_integer()} + + use GenServer + @name __MODULE__ + + @spec memory() :: memory() + def memory do + :ets.lookup_element(@name, :memory, 2) + end + + @doc false + def start_link(_opts) do + GenServer.start_link(__MODULE__, :ok, name: @name) + end + + @impl true + def init(:ok) do + :ets.new(@name, [:set, :named_table, :protected]) + measure_and_schedule() + {:ok, %{}} + end + + @impl true + def handle_info(:measure, state) do + measure_and_schedule() + {:noreply, state} + end + + defp measure_and_schedule() do + memory = :memsup.get_system_memory_data() + :ets.insert(@name, {:memory, %{total: memory[:total_memory], free: memory[:free_memory]}}) + Process.send_after(self(), :measure, 15000) + end +end diff --git a/lib/livebook/utils.ex b/lib/livebook/utils.ex index 7b638ae4c..20c89c436 100644 --- a/lib/livebook/utils.ex +++ b/lib/livebook/utils.ex @@ -3,8 +3,6 @@ defmodule Livebook.Utils do @type id :: binary() - @type system_memory :: %{total: non_neg_integer(), free: non_neg_integer()} - @doc """ Generates a random binary id. """ @@ -371,15 +369,6 @@ defmodule Livebook.Utils do |> Enum.join("\n") end - @doc """ - Fetches the total and free memory of the system - """ - @spec fetch_system_memory() :: system_memory() - def fetch_system_memory() do - memory = :memsup.get_system_memory_data() - %{total: memory[:total_memory], free: memory[:free_memory]} - end - def format_bytes(bytes) when is_integer(bytes) do cond do bytes >= memory_unit(:TB) -> format_bytes(bytes, :TB) diff --git a/lib/livebook_web/live/home_live/session_list_component.ex b/lib/livebook_web/live/home_live/session_list_component.ex index 1e58fc6ef..8fde891f7 100644 --- a/lib/livebook_web/live/home_live/session_list_component.ex +++ b/lib/livebook_web/live/home_live/session_list_component.ex @@ -1,7 +1,7 @@ defmodule LivebookWeb.HomeLive.SessionListComponent do use LivebookWeb, :live_component - import Livebook.Utils, only: [format_bytes: 1, fetch_system_memory: 0] + import Livebook.Utils, only: [format_bytes: 1] import LivebookWeb.SessionHelpers, only: [uses_memory?: 1] @impl true @@ -24,8 +24,11 @@ defmodule LivebookWeb.HomeLive.SessionListComponent do socket = socket |> assign(assigns) - |> assign(sessions: sessions, show_autosave_note?: show_autosave_note?) - |> assign(memory: memory_info(sessions)) + |> assign( + sessions: sessions, + show_autosave_note?: show_autosave_note?, + memory: memory_info() + ) {:ok, socket} end @@ -38,15 +41,15 @@ defmodule LivebookWeb.HomeLive.SessionListComponent do
+ <%= format_bytes(@session.memory_usage.system.free) %> + available out of + <%= format_bytes(@session.memory_usage.system.total) %> +