From 471a50ef1391f6399664f8b992da7a67c32c8b86 Mon Sep 17 00:00:00 2001 From: Raimo Niskanen Date: Tue, 26 Apr 2016 16:49:47 +0200 Subject: Fix hibernation subtlety --- system/doc/design_principles/statem.xml | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'system/doc') diff --git a/system/doc/design_principles/statem.xml b/system/doc/design_principles/statem.xml index a4b8fb06a0..ca0fce55e2 100644 --- a/system/doc/design_principles/statem.xml +++ b/system/doc/design_principles/statem.xml @@ -1448,4 +1448,75 @@ format_status(Opt, [_PDict,State,Data]) ->

+ + +
+ Hibernation +

+ If you have many servers in one node + and they have some state(s) in their lifetime in which + the servers can be expected to idle for a while, + and the amount of heap memory all these servers need + is a problem; then it is possible to minimize + the memory footprint of a server by hibernating it through + + proc_lib:hibernate/3. + +

+ +

+ To hibernate a process is rather costly. See + + erlang:hibernate/3. + + It is in general not something you want to do + after every event. +

+
+

+ We can in this example hibernate in the {open,_} state + since what normally happens in that state is that + the state timeout after a while + triggers a transition to {locked,_}: +

+ + case {EventType,EventContent} of + {internal,enter} -> + Tref = erlang:start_timer(10000, self(), lock), + do_unlock(), + {keep_state,Data#{timer := Tref},[hibernate]}; +... + ]]> +

+ The + + [hibernate] + + action list on the last line + when entering the {open,_} state is the only change. + If any event arrives in the {open,_}, state we + do not bother to re-hibernate, so the server stays + awake after any event. +

+

+ To change that we would need to insert + the hibernate action in more places, + for example for the state independent set_lock_button + and code_length operations that then would have to + be aware of using hibernate while in the + {open,_} state which would clutter the code. +

+

+ This server probably does not use an amount of + heap memory worth hibernating for. + To gain anything from hibernation your server would + have to actually produce some garbage during callback execution, + for which this example server may serve as a bad example. +

+
+ -- cgit v1.2.3