Hot code loading

From Openmoko

(Difference between revisions)
Jump to: navigation, search
(To change a running erlang system)
(Example Code)
 
(15 intermediate revisions by one user not shown)
Line 1: Line 1:
== To change a running erlang system ==
+
== To change a running [[Erlang]] system ==
 +
For most of us it's a cool experience to be able to inject compiled code into a running VM and instantly watch the system change.
  
=== Start erlang as a node on your host===
+
=== Start an erlang node on your neo===
 
This is my /etc/X11/Xsession.d/80zhone. As you can see the erlang node's got a name "-name neo@192.168.0.202" (a running erlang virtual machine is called an erlang node). And it's got a cookie "-setcookie SFEWRG34AFDSGAFG35235". This is all that's needed on the neo.
 
This is my /etc/X11/Xsession.d/80zhone. As you can see the erlang node's got a name "-name neo@192.168.0.202" (a running erlang virtual machine is called an erlang node). And it's got a cookie "-setcookie SFEWRG34AFDSGAFG35235". This is all that's needed on the neo.
  
Line 12: Line 13:
 
</pre>
 
</pre>
  
 
+
=== Start an erlang node on your host===
 
Every erlang node in your network should have a unique name, and a common cookie. Start an erlang node on your host PC with a unique name and the common cookie:
 
Every erlang node in your network should have a unique name, and a common cookie. Start an erlang node on your host PC with a unique name and the common cookie:
 
<pre>
 
<pre>
Line 18: Line 19:
 
</pre>
 
</pre>
  
From your host erlang console try
+
Once again - first make sure you can access your neo from the host with ping, ssh ,rcp etc. Then, from your host erlang console try
 
<pre>
 
<pre>
 
net_adm:ping('neo@192.168.0.202').  
 
net_adm:ping('neo@192.168.0.202').  
Line 31: Line 32:
  
 
=== HotLoad the code ===
 
=== HotLoad the code ===
To carry out Hot Code Loading type - on your host erlang console - and make sure erlang can find your module 'your_modulename':
+
Make sure erlang can find the compiled module 'your_modulename' on the local file system.
 +
 
 +
To carry out Hot Code Loading type (on your host erlang console):
 
<pre>
 
<pre>
 
net_adm:ping('neo@192.168.0.202').  
 
net_adm:ping('neo@192.168.0.202').  
 
nl(your_modulename).
 
nl(your_modulename).
 
</pre>
 
</pre>
 +
nl() is short for network_load and will remotely load a compiled erlang module on every connected node on your network.
 +
 
Substitute your_modulename with the name of the module you want to load into the virtual machine on your neo. The response should be 'abcast' if sucessful. If not the response is 'error'.
 
Substitute your_modulename with the name of the module you want to load into the virtual machine on your neo. The response should be 'abcast' if sucessful. If not the response is 'error'.
  
The ping command is only necessary the first time to let your host node find your neo node. When you are finished, don't forget to copy the compiled erlang file to your neo, hot code loading does not copy your beam file to the file system.
+
The ping command is only necessary the first time to let your host node find your neo node. When you are finished, don't forget to copy the compiled erlang file to your neo, hot code loading does not copy your beam file to the remote file system.
  
===Example Code===
+
==Example Code==
There is only one thing you must know when using hot code loading, and that is what makes your code reload. Every time the erlang VM finds a module_name:function_name() in your code it looks for (and uses) the most recent version it has loaded. So - just export your loop function and preface it with the module name:
+
There is only one thing you must know when using hot code loading, and that is what makes your code reload. Every time the erlang VM finds a module_name:function_name() in your code it looks for (and uses) the most recent version it has loaded. So - just export your loop function and preface it in your calls with the module name or with the macro ?MODULE:
  
 
<pre>
 
<pre>
 
-export([start/0,loop/10]).
 
-export([start/0,loop/10]).
  
main:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot,Hotspots) ->
+
loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot,Hotspots) ->
 
     receive
 
     receive
 
         {event,_,configureNotify,_} ->
 
         {event,_,configureNotify,_} ->
Line 53: Line 58:
 
             Spot1 = finger_at({X,Y},Hotspots),
 
             Spot1 = finger_at({X,Y},Hotspots),
 
             Pid ! {touching,Spot1},
 
             Pid ! {touching,Spot1},
             ?MODULE:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot1,Hotspots);
+
             ?MODULE:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot1,Hotspots)
 
end.
 
end.
 
</pre>
 
</pre>
 +
 +
[[Category:Application Developer]]

Latest revision as of 22:13, 9 December 2008

Contents

[edit] To change a running Erlang system

For most of us it's a cool experience to be able to inject compiled code into a running VM and instantly watch the system change.

[edit] Start an erlang node on your neo

This is my /etc/X11/Xsession.d/80zhone. As you can see the erlang node's got a name "-name neo@192.168.0.202" (a running erlang virtual machine is called an erlang node). And it's got a cookie "-setcookie SFEWRG34AFDSGAFG35235". This is all that's needed on the neo.

#!/bin/sh -e
#zhone > /tmp/zhone.log 2>&1 &
/home/root/cean/start.sh -setcookie SFEWRG34AFDSGAFG35235 -name neo@192.168.0.202 -noshell -pa /home/root/trunk -s main start > debug.txt
renice -3 $!
exit 0

[edit] Start an erlang node on your host

Every erlang node in your network should have a unique name, and a common cookie. Start an erlang node on your host PC with a unique name and the common cookie:

erl -setcookie SFEWRG34AFDSGAFG35235 -name 'host@192.168.0.200'

Once again - first make sure you can access your neo from the host with ping, ssh ,rcp etc. Then, from your host erlang console try

net_adm:ping('neo@192.168.0.202'). 

The response should be 'pong' if you have a successful connection - if not, the response is 'pang'

Then try the shell command

nodes().

The response should be a list of all known nodes in your network.

[edit] HotLoad the code

Make sure erlang can find the compiled module 'your_modulename' on the local file system.

To carry out Hot Code Loading type (on your host erlang console):

net_adm:ping('neo@192.168.0.202'). 
nl(your_modulename).

nl() is short for network_load and will remotely load a compiled erlang module on every connected node on your network.

Substitute your_modulename with the name of the module you want to load into the virtual machine on your neo. The response should be 'abcast' if sucessful. If not the response is 'error'.

The ping command is only necessary the first time to let your host node find your neo node. When you are finished, don't forget to copy the compiled erlang file to your neo, hot code loading does not copy your beam file to the remote file system.

[edit] Example Code

There is only one thing you must know when using hot code loading, and that is what makes your code reload. Every time the erlang VM finds a module_name:function_name() in your code it looks for (and uses) the most recent version it has loaded. So - just export your loop function and preface it in your calls with the module name or with the macro ?MODULE:

-export([start/0,loop/10]).

loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot,Hotspots) ->
    receive
        {event,_,configureNotify,_} ->
            ?MODULE:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot,Hotspots);
	{event,_, buttonPress, {_,X,Y,_,_}} ->
            Spot1 = finger_at({X,Y},Hotspots),
            Pid ! {touching,Spot1},
            ?MODULE:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot1,Hotspots)
end.
Personal tools

To change a running erlang system

Start erlang as a node on your host

This is my /etc/X11/Xsession.d/80zhone. As you can see the erlang node's got a name "-name neo@192.168.0.202" (a running erlang virtual machine is called an erlang node). And it's got a cookie "-setcookie SFEWRG34AFDSGAFG35235". This is all that's needed on the neo.

#!/bin/sh -e
#zhone > /tmp/zhone.log 2>&1 &
/home/root/cean/start.sh -setcookie SFEWRG34AFDSGAFG35235 -name neo@192.168.0.202 -noshell -pa /home/root/trunk -s main start > debug.txt
renice -3 $!
exit 0


Every erlang node in your network should have a unique name, and a common cookie. Start an erlang node on your host PC with a unique name and the common cookie:

erl -setcookie SFEWRG34AFDSGAFG35235 -name 'host@192.168.0.200'

From your host erlang console try

net_adm:ping('neo@192.168.0.202'). 

The response should be 'pong' if you have a successful connection - if not, the response is 'pang'

Then try the shell command

nodes().

The response should be a list of all known nodes in your network.

HotLoad the code

To carry out Hot Code Loading type - on your host erlang console - and make sure erlang can find your module 'your_modulename':

net_adm:ping('neo@192.168.0.202'). 
nl(your_modulename).

Substitute your_modulename with the name of the module you want to load into the virtual machine on your neo. The response should be 'abcast' if sucessful. If not the response is 'error'.

The ping command is only necessary the first time to let your host node find your neo node. When you are finished, don't forget to copy the compiled erlang file to your neo, hot code loading does not copy your beam file to the file system.

Example Code

There is only one thing you must know when using hot code loading, and that is what makes your code reload. Every time the erlang VM finds a module_name:function_name() in your code it looks for (and uses) the most recent version it has loaded. So - just export your loop function and preface it with the module name:

-export([start/0,loop/10]).

main:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot,Hotspots) ->
    receive
        {event,_,configureNotify,_} ->
            ?MODULE:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot,Hotspots);
	{event,_, buttonPress, {_,X,Y,_,_}} ->
            Spot1 = finger_at({X,Y},Hotspots),
            Pid ! {touching,Spot1},
            ?MODULE:loop(Pid,Display,Win,Buttons,Dbus,F_hash,Pos,Number,Spot1,Hotspots);
end.