From 3ab4e63441099b510660ac8666f3b2224da323b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Casta=C3=B1o?= Date: Wed, 8 Nov 2023 00:25:19 +0100 Subject: [PATCH] System fetch instrument allocators --- lib/phoenix/live_dashboard/system_info.ex | 12 ++++++++++++ .../phoenix/live_dashboard/system_info_test.exs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/phoenix/live_dashboard/system_info.ex b/lib/phoenix/live_dashboard/system_info.ex index 684a2d4c..4211c698 100644 --- a/lib/phoenix/live_dashboard/system_info.ex +++ b/lib/phoenix/live_dashboard/system_info.ex @@ -115,6 +115,10 @@ defmodule Phoenix.LiveDashboard.SystemInfo do :rpc.call(node, __MODULE__, :memory_allocators_callback, [max_carrier_sizes]) end + def fetch_instrument_allocations(node) do + :rpc.call(node, __MODULE__, :instrument_allocations_callback, []) + end + ## System callbacks @doc false @@ -269,6 +273,14 @@ defmodule Phoenix.LiveDashboard.SystemInfo do end) end + def instrument_allocations_callback() do + if Code.ensure_loaded?(:instrument) do + :instrument.allocations() + else + {:error, :instrument_not_available} + end + end + ## Process Callbacks @processes_keys [ diff --git a/test/phoenix/live_dashboard/system_info_test.exs b/test/phoenix/live_dashboard/system_info_test.exs index 3d312a38..0f6ce469 100644 --- a/test/phoenix/live_dashboard/system_info_test.exs +++ b/test/phoenix/live_dashboard/system_info_test.exs @@ -312,4 +312,21 @@ defmodule Phoenix.LiveDashboard.SystemInfoTest do ] = list end end + + describe "fetch_instrument_allocations" do + test "works" do + assert {:ok, {128, 0, allocs}} = SystemInfo.fetch_instrument_allocations(node()) + + assert %{ + system: %{binary: binary_tuple, nif_internal: nif_tuple, port: port_tuple}, + prim_file: %{}, + tcp_inet: %{drv_internal: drv_tuple} + } = allocs + + assert tuple_size(binary_tuple) == 18 + assert tuple_size(nif_tuple) == 18 + assert tuple_size(port_tuple) == 18 + assert tuple_size(drv_tuple) == 18 + end + end end