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
@ferd
ferd / gist:3274480
Created August 6, 2012 13:29
copy/paste in the shell, call Biggest(Milliseconds, N)
F = fun() -> [{Pid, {Reds, Curr, Init}} || Pid <- processes(),
{_, Reds} <- [process_info(Pid, reductions)],
{_, Curr} <- [process_info(Pid, current_function)],
{_, Init} <- [process_info(Pid, initial_call)]] end.
Fetch = fun(T) -> A = F(), timer:sleep(T), B= F(), {A,B} end.
Build = fun({A,B}) ->
D = dict:from_list(A),
lists:foldl(fun({Pid,{Red,Cur,Init}}, Dict) ->
%%% Macro definitions for lazy (or rather delayed) evaluation.
%% Force and delay are used in combination to delay evaluation to only
%% when strictly needed:
%% Promise = ?delay(expensive_computation(A,B,C)),
%% Value = ?force(Promise).
%%
%% These macros should be used with extreme precaution when sending a
%% function to another process as it relies on closures and the sharing of
%% information is lost when copying. So is memoization. Therefore, lazy
@ferd
ferd / app_deps.erl
Created September 23, 2012 05:33
Quick escript to generate a visualization of app dependencies in Erlang/OTP.
%%% Run with 'escript app_deps.erl'
%%% Change the path in filelib:wildcard/1 as required to capture all
%%% your dependencies.
%%%
%%% Rectangular nodes will represent library apps (no processes involved)
%%% and the circular nodes will represent regular apps. An arrow going from
%%% 'A -> B' means 'A depends on B'.
%%%
%%% This script depends on graphviz being present on the system.
-module(app_deps).
@ferd
ferd / erlang-shell-search-history.patch
Created December 20, 2012 13:59
Add search to Erlang shell's history Search mode can be entered by pressing ctrl-r. Enter terms and press ctrl-r again to search backwards, or ctrl-s to then search forward (if you terminal doesn't eat up that one). Press enter to execute the line, or use tab, arrow keys, or other control sequences (^D, ^K, etc.) to exit search mode while remain…
From d98e6c32d44e00f954c3912b08c6ebf48d55729c Mon Sep 17 00:00:00 2001
From: Fred Hebert <mononcqc@ferd.ca>
Date: Thu, 20 Dec 2012 08:41:33 -0500
Subject: [PATCH] Add search to Erlang shell's history
Search mode can be entered by pressing ctrl-r. Enter terms and press
ctrl-r again to search backwards, or ctrl-s to then search forward (if
you terminal doesn't eat up that one). Press enter to execute the line,
or use tab, arrow keys, or other control sequences (^D, ^K, etc.) to
exit search mode while remaining on the last found line.
@ferd
ferd / behaviour_parse.erl
Created February 19, 2013 17:09
Parse transform to support -spec and behaviour_info in Erlang
-module(behaviour_parse).
-export([parse_transform/2]).
-define(CUTOFF, "2.15"). % R15B
parse_transform(AST, _Opts) ->
[Vsn] = [X || {kernel,_,X} <- application:which_applications()],
walk_ast(if Vsn >= ?CUTOFF -> spec;
Vsn < ?CUTOFF -> func
end,
AST).
@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,
@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: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:5828217
Last active December 18, 2015 18:49
λ self → git clone git@github.com:ferd/vmstats.git
Cloning into 'vmstats'...
Identity added: /Users/ferd/.ssh/id_rsa (/Users/ferd/.ssh/id_rsa)
remote: Counting objects: 83, done.
remote: Compressing objects: 100% (65/65), done.
remote: Total 83 (delta 44), reused 57 (delta 18)
Receiving objects: 100% (83/83), 113.39 KiB, done.
Resolving deltas: 100% (44/44), done.
λ self → cd vmstats
λ vmstats → master → ls
@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()),