|
| 1 | +defmodule Neurow.Observability.SystemStats do |
| 2 | + use Prometheus.Metric |
| 3 | + |
| 4 | + def setup() do |
| 5 | + Gauge.declare( |
| 6 | + name: :memory_usage, |
| 7 | + help: "Memory usage" |
| 8 | + ) |
| 9 | + |
| 10 | + Boolean.declare( |
| 11 | + name: :stopping, |
| 12 | + help: "The node is currently stopping" |
| 13 | + ) |
| 14 | + |
| 15 | + Gauge.set([name: :memory_usage], 0) |
| 16 | + Boolean.set([name: :stopping], false) |
| 17 | + |
| 18 | + Periodic.start_link( |
| 19 | + run: fn -> Gauge.set([name: :memory_usage], :recon_alloc.memory(:usage)) end, |
| 20 | + every: :timer.seconds(10) |
| 21 | + ) |
| 22 | + end |
| 23 | + |
| 24 | + def report_shutdown() do |
| 25 | + Boolean.set([name: :stopping], true) |
| 26 | + end |
| 27 | + |
| 28 | + defmodule ProcessesStats do |
| 29 | + defstruct [ |
| 30 | + :name_or_initial_func, |
| 31 | + :current_func, |
| 32 | + process_count: 0, |
| 33 | + memory: 0, |
| 34 | + message_queue_len: 0 |
| 35 | + ] |
| 36 | + end |
| 37 | + |
| 38 | + # Group process by name or initial function, and current function. |
| 39 | + # Then sort by memory usage and return the top consuming processe groups |
| 40 | + def process_groups(result_count \\ 50) do |
| 41 | + Process.list() |
| 42 | + |> Enum.reduce(%{}, fn pid, acc -> |
| 43 | + {name_or_initial_func, current_func} = grouping_attributes(pid) |
| 44 | + |
| 45 | + Map.update( |
| 46 | + acc, |
| 47 | + {name_or_initial_func, current_func}, |
| 48 | + %ProcessesStats{ |
| 49 | + name_or_initial_func: name_or_initial_func, |
| 50 | + current_func: current_func |
| 51 | + }, |
| 52 | + fn current_stats -> |
| 53 | + process_info = Process.info(pid, [:memory, :message_queue_len]) |
| 54 | + |
| 55 | + %{ |
| 56 | + current_stats |
| 57 | + | process_count: current_stats.process_count + 1, |
| 58 | + memory: current_stats.memory + (process_info[:memory] || 0), |
| 59 | + message_queue_len: |
| 60 | + current_stats.message_queue_len + (process_info[:message_queue_len] || 0) |
| 61 | + } |
| 62 | + end |
| 63 | + ) |
| 64 | + end) |
| 65 | + |> Map.values() |
| 66 | + |> Enum.sort(&(&1.memory > &2.memory)) |
| 67 | + |> Enum.take(result_count) |
| 68 | + end |
| 69 | + |
| 70 | + defp mfa_to_string({module, function, arity}) do |
| 71 | + "#{module}:#{function}/#{arity}" |
| 72 | + end |
| 73 | + |
| 74 | + defp grouping_attributes(pid) do |
| 75 | + name_or_initial_func = |
| 76 | + case Process.info(pid, [:registered_name, :dictionary, :initial_call]) do |
| 77 | + [{:registered_name, name} | _rest] when is_atom(name) -> |
| 78 | + name |
| 79 | + |
| 80 | + [{:registered_name, [first_name | _other_names]}, _rest] -> |
| 81 | + first_name |
| 82 | + |
| 83 | + [ |
| 84 | + {:registered_name, []}, |
| 85 | + {:dictionary, [{:"$initial_call", initial_call} | _rest_dictionary]} | _rest |
| 86 | + ] -> |
| 87 | + mfa_to_string(initial_call) |
| 88 | + |
| 89 | + [ |
| 90 | + {:registered_name, []}, |
| 91 | + {:dictionary, _rest_dictionary}, |
| 92 | + {:initial_call, initial_call} |
| 93 | + ] -> |
| 94 | + mfa_to_string(initial_call) |
| 95 | + |
| 96 | + _ -> |
| 97 | + :undefined |
| 98 | + end |
| 99 | + |
| 100 | + case Process.info(pid, :current_function) do |
| 101 | + {:current_function, current_function} -> |
| 102 | + {name_or_initial_func, mfa_to_string(current_function)} |
| 103 | + |
| 104 | + nil -> |
| 105 | + {name_or_initial_func, :undefined} |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments