The thing Erlang does that almost nothing else does is let you inspect and change a system while it is serving traffic. That capability is the reason to put up with the syntax, and it is also the easiest way to take production down if you are careless.
Hot code upgrade. The BEAM holds two versions of a module at once - current and old. Loading a new one makes the previous version old; processes keep running the old code until they make a fully qualified call (?MODULE:loop(State) rather than loop(State)), which jumps to the new version. Load a third version and anything still on the first is killed. For a real release, appup files describe how to get from one version to the next and relup generates the upgrade instructions, with code_change/3 on each gen_server migrating its state. It works, it is genuinely useful for long-lived connections you cannot drop, and most teams today deploy with a rolling restart instead because it is simpler and the cluster already tolerates a node disappearing.
Attaching to a running node is bin/myapp remote_console, which starts a shell as a hidden node joined to the live one. From there observer:start() opens a GUI with every process, its memory, its mailbox length and the full supervision tree. On a headless server recon gives you the same information as functions.
Tracing is the sharp tool. dbg can trace every call to a function across the cluster, with arguments and return values, and that is exactly the problem: trace something hot with no limit and the trace messages themselves will overwhelm the node. The rule is to always bound it - a match specification that narrows what is captured, and a message or time limit. recon_trace:calls/2 has those limits built in and is the one to use in production. redbug is the other safe choice; it defaults to stopping after a count or a timeout.
The leak to look for first is binaries. Phase two covered the mechanism: a sub-binary keeps its parent alive. In production this shows as a process with modest heap and enormous binary memory, and recon:bin_leak/1 finds it by forcing garbage collection everywhere and reporting who freed the most. The other usual suspects are a growing mailbox - recon:proc_count(message_queue_len, 10) - and ETS tables nobody is pruning.
%% ---------------- Attaching to a live node ----------------
%%
%% bin/myapp remote_console % a shell on the RUNNING node
%% Ctrl-G then q % detach WITHOUT stopping it.
%% % q() would stop the node. Do not.
%%
%% observer:start(). % the GUI, if you have a display:
%% % processes, memory, ETS, the whole
%% % supervision tree, live.
%%
%% Headless, the same information as functions:
%%
%% erlang:memory().
%% erlang:system_info(process_count).
%% length(processes()).
%% i(). % every process on the node
%% ---------------- recon: the production toolkit ----------------
%%
%% recon:proc_count(memory, 10). % top 10 by memory
%% recon:proc_count(message_queue_len, 10). % top 10 by mailbox
%% recon:proc_count(reductions, 10). % top 10 by work done
%%
%% recon:info(Pid). % everything about one
%%
%% recon:bin_leak(10).
%% Forces a GC on every process and reports which freed the most
%% binary memory. The processes at the top of that list are the
%% ones holding sub-binaries alive. This is the first thing to run
%% when memory is climbing and the heaps look small.
%%
%% recon_alloc:memory(usage). % allocator fragmentation
%% recon:node_stats_print(10, 1000). % live node statistics
%% ---------------- Tracing, with limits ----------------
%%
%% recon_trace has the limits BUILT IN. Use this one:
%%
%% recon_trace:calls({bank_account, withdraw, '_'}, 10).
%% Trace withdraw/N, stop after 10 messages.
%%
%% recon_trace:calls({bank_account, withdraw, fun(_) -> return_trace() end},
%% {10, 5000}).
%% With return values, 10 messages, within 5 seconds.
%%
%% recon_trace:clear().
%%
%% dbg is more powerful and has NO limits. On a hot function it will
%% flood the node with trace messages and take it down:
%%
%% dbg:tracer(),
%% dbg:p(all, c),
%% dbg:tpl(bank_account, withdraw, cx), % cx = calls and returns
%% ...
%% dbg:stop_clear(). % ALWAYS clean up
%%
%% redbug is the other safe option - it stops after a count or a time:
%%
%% redbug:start("bank_account:withdraw->return", [{time, 10000},
%% {msgs, 100}]).
%% ---------------- Hot code upgrade ----------------
%%
%% The BEAM holds TWO versions of a module: current and old.
%%
%% code:load_file(my_module). % new becomes current, current
%% % becomes old
%% code:soft_purge(my_module). % remove old, only if unused
%% code:purge(my_module). % remove old, KILLING anything
%% % still running it
%%
%% A process keeps running the version it started in until it makes a
%% FULLY QUALIFIED call. This is why server loops are written:
%%
%% loop(State) ->
%% receive
%% upgrade -> ?MODULE:loop(State); % jumps to the NEW code
%% Msg -> loop(handle(Msg, State)) % stays in THIS one
%% end.
%%
%% Load a third version and anything still on the first is killed.
%%
%% For a release, appup and relup do this properly:
%%
%% {"1.1.0",
%% [{"1.0.0", [{load_module, bank_account},
%% {update, bank_sup, supervisor},
%% {update, bank_account, {advanced, []}}]}],
%% [{"1.0.0", [{load_module, bank_account}]}]}.
%%
%% and code_change/3 migrates each gen_server's state:
%%
%% code_change({down, _Vsn}, State, _Extra) -> {ok, downgrade(State)};
%% code_change(_OldVsn, {state, A, B}, _Extra) ->
%% {ok, #state{a = A, b = B, c = default}}. % a new field
%%
%% Worth knowing it exists and that it works. Most teams deploy with a
%% rolling restart, because the cluster already survives a node going
%% away and that is far less to get right.
Discussion
No questions on this topic yet. Sign in to ask the first one.