Skip to content

Instantly share code, notes, and snippets.

View ferd's full-sized avatar
☢️
This place is not a place of honor… no highly esteemed deed is commemorated here

Fred Hebert ferd

☢️
This place is not a place of honor… no highly esteemed deed is commemorated here
View GitHub Profile
@starbelly
starbelly / rebar3_private_repo_packages_test_steps.md
Last active November 18, 2021 14:11
How to test fetching and publishing private packages with rebar3 and a local hexpm instance.
@guillermo
guillermo / my_app.sh
Created January 15, 2014 08:27
This is a unix wrapper around the erlang vm.
#!/bin/bash
# This is a unix wrapper around the erlang vm. It provides the following functionality:
#
# * Spawns in foreground
# * Handle SIGHUP and call RELOADFUNC
# * Handle SIGTERM SIGQUIT and SIGINT telling to the vm to quit
# * Dies if the vm dies (for example kernel killing because out of memory)
#
# Forks and improvements are welcome.
cat /erl_crash.dump | grep -2 '=proc:' | grep 'Spawned as:' | cut -d: -f2-3 | sort | uniq -c | sort -rn
12433 cowboy_protocol:init/4
1033 proc_lib:init_p/5
1024 ranch_acceptor:loop/3
16 application_master:start_it/4
14 erlang:apply/2
10 inet_tcp_dist:do_setup/6
4 inet_tcp_dist:do_accept/6
4 dlhttpc_client:request/10
1 net_kernel:ticker/2
# Parse Erlang Crash Dumps and correlate mailbox size to the currently running
# function.
#
# Once in the procs section of the dump, all processes are displayed with
# =proc:<0.M.N> followed by a list of their attributes, which include the
# message queue length and the program counter (what code is currently
# executing).
BEGIN {
threshold = 10000 # mailbox size
procs = 0 # are we in the =procs entries?
@ferd
ferd / refc_leak.erl
Created July 18, 2013 12:32
Find Erlang processes that may be leaking refc binaries
f(MostLeaky).
MostLeaky = fun(N) ->
lists:sublist(
lists:usort(
fun({K1,V1},{K2,V2}) -> {V1,K1} =< {V2,K2} end,
[try
{_,Pre} = erlang:process_info(Pid, binary),
erlang:garbage_collect(Pid),
{_,Post} = erlang:process_info(Pid, binary),
{Pid, length(Post)-length(Pre)}
@ferd
ferd / cdumpbin.erl
Created July 12, 2013 19:05
Convert a crash dump Refc binary back to a regular binary. Uses the erl_eval module to evaluate the 16#... hex conversion much faster than naive string handling would do it in this little space.
%% Convert a crashdump binary back into a regular binary
%% Crash dump binary:
%% =binary:CFE75808 % <- reference to refc binary
%% CA:2A31300D0A24350D0A484D5345540D0... % <- actual binary
CDumpBin = fun(Str) ->
[Len,Num] = string:tokens(Str, ":"),
Src= lists:flatten(["<<16#",Num,":(16#",Len,"*8)>>."]),
{ok, Tokens, _}=erl_scan:string(Src),
{ok, [Form]} = erl_parse:parse_exprs(Tokens),
{value, Val, _} = erl_eval:expr(Form, erl_eval:new_bindings()),
@ferd
ferd / gist:5783802
Created June 14, 2013 17:38
Compare runtime, run queue, and scheduler business
erlang:system_flag(scheduler_wall_time, true),
f(WallTimeDiff),
WallTimeDiff = fun(T1,T2) -> [trunc(100*((Active2-Active1)/(Total2-Total1)))/100 || {{I, Active1, Total1}, {I, Active2, Total2}} <- lists:zip(lists:sort(T1),lists:sort(T2))] end,
f(F), put(stime, erlang:statistics(scheduler_wall_time)),
F = fun(F,N) -> Old=get(stime), New=erlang:statistics(scheduler_wall_time), put(stime,New), io:format("rt:~p\trq:~p\t\tsched:~w~n", [element(2,erlang:statistics(runtime)), erlang:statistics(run_queue), WallTimeDiff(Old,New)]), timer:sleep(N), F(F,N) end.
%% F(F, IntervalInMS).
@ferd
ferd / gist:5708689
Created June 4, 2013 19:19
loop that displays stats for a node
f(Init), f(Loop), f(Stats), f(ShowStats),
Init = fun(Delay) ->
Ref = erlang:start_timer(Delay, self(), '#delay'),
{{input,In},{output,Out}} = erlang:statistics(io),
PrevGC = erlang:statistics(garbage_collection),
{{Delay,Ref}, {In,Out}, PrevGC}
end,
Loop = fun(Self,F,N,{{D,R},{OldIn,OldOut},{OldGCs,OldWords,_}}) ->
receive
{timeout,R,'#delay'} ->
@ferd
ferd / gist:5694838
Last active December 18, 2015 00:09
f(Window).
Window = fun(AttrName, Time,NumProcs) ->
Attrs = fun(Name) ->
[{Pid, {Attr, Curr, Init}}
|| Pid <- processes() -- [self()],
[{_, Attr}, {_, Curr}, {_, Init}] <-
[process_info(Pid, [Name, current_function, initial_call])]]
end,
F = fun() -> Attrs(AttrName) end,