<div dir="ltr">I have been tracing the boot progress of a minimal release (generated with Mix) to understand who and when <span class="" id=":tn.1" tabindex="-1" style="">preloads</span> modules and runs on_load handlers.<div><div><br></div><div>The source code of application.<span class="" id=":tn.2" tabindex="-1" style="">erl</span> and application_controller.<span class="" id=":tn.3" tabindex="-1" style="">erl</span> seem to suggest that (-<span class="" id=":tn.4" tabindex="-1" style="">init</span>_debug notation)</div><div><br></div><div>    {apply,{application,load,...}</div><div><br></div><div>basically loads the application resource file and stores stuff in <span class="" id=":tn.5" tabindex="-1" style="">ETS</span> tables, but object code does not seem to be loaded (that coincides with the documentation of application:load/1, but contradicts at least two books).</div><div><br></div><div>I have added an on_load callback in a module that prints a trace, and that trace is shown after *all* applications have been loaded, after kernel has been started, and before <span class="" id=":tn.6" tabindex="-1" style="">stdlib</span> is started.</div><div><br></div><div>I thought that meant kernel:start_boot preloaded the modules. But I have read some of the initialization of kernel and I believe the callback is *not* called when the module has been loaded into the <span class="" id=":tn.7" tabindex="-1" style="">VM</span>, but with this ad-<span class="" id=":tn.8" tabindex="-1" style="">hoc</span> line in the <span class="" id=":tn.9" tabindex="-1" style="">init</span>/1 of kernel_sup:</div><div>      https://<span class="" id=":tn.10" tabindex="-1" style="">github</span>.com/<span class="" id=":tn.11" tabindex="-1" style="">erlang</span>/<span class="" id=":tn.12" tabindex="-1" style="">otp</span>/blob/db58a0c04ca183de5e5436e0ae97e3f109a458fe/lib/kernel/<span class="" id=":tn.13" tabindex="-1" style="">src</span>/kernel.<span class="" id=":tn.14" tabindex="-1" style="">erl</span>#L224</div><div><br></div><div>On the other hand, I have read the source code of <span class="" id=":tn.15" tabindex="-1" style="">init</span>.<span class="" id=":tn.16" tabindex="-1" style="">erl</span> and looks like the thing happens when processing <span class="" id=":tn.17" tabindex="-1" style="">primLoad</span>:</div><div><br></div><div>    https://<span class="" id=":tn.18" tabindex="-1" style="">github</span>.com/<span class="" id=":tn.19" tabindex="-1" style="">erlang</span>/<span class="" id=":tn.20" tabindex="-1" style="">otp</span>/blob/db58a0c04ca183de5e5436e0ae97e3f109a458fe/<span class="" id=":tn.21" tabindex="-1" style="">erts</span>/preloaded/<span class="" id=":tn.22" tabindex="-1" style="">src</span>/<span class="" id=":tn.23" tabindex="-1" style="">init</span>.<span class="" id=":tn.24" tabindex="-1" style="">erl</span>#L899</div><div><br></div><div>which goes before all application loading in a conventional generated boot script.</div><div><br></div><div>So, I've come with this tentative <span class="" id=":tn.25" tabindex="-1" style="">workflow</span>:</div><div><br></div><div>    * <span class="" id=":tn.26" tabindex="-1" style="">primLoad</span> loads all object code into the BEAM. In a normal boot script that means loading all modules of all bundled applications and included applications.</div><div><br></div><div>    * Loading applications just processes and stores the application resource file.</div><div><br></div><div>    * On boot, on_load handlers do not actually run when the object code is loaded into the BEAM, but much later, when kernel_sup is initialized as part of starting the kernel application (which I guess makes sense because you want to run that code when the callback is able to execute with enough stuff bootstrapped).</div><div><br></div><div>My <span class="" id=":tn.27" tabindex="-1" style="">walkthrough</span> has not been exhaustive, though. Can anyone confirm/refute that?</div><div><br></div></div></div>