Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'actor-startkilltime' of https://github.com/Takishipp/simgrid into Takis...
[simgrid.git] / doc / doxygen / java.doc
1 /*! @page MSG_Java Java Bindings
2
3 @tableofcontents
4
5 This section describes jMSG, the Java API to Simgrid. This API mimicks 
6 @ref MSG_API "MSG", which is a simple yet somehow realistic interface.
7 <b>The full [javadoc](javadoc/index.html) is available.</b>
8
9 Most of the documentation of the @ref MSG_API "MSG API" in C applies
10 directly to the Java bindings (any divergence is seen as a bug that we
11 should fix). MSG structures are mapped to Java objects as expected,
12 and the MSG functions are methods in these objects.
13
14 @section java_install How to install the Java bindings
15
16 This is documented in the @ref install_java Section.
17
18 @section java_use How to use the Java bindings
19
20 In most cases, you can use the SimGrid bindings as if it was a Java
21 library:
22
23 ~~~~{.sh}
24 $ javac -classpath .:path/to/simgrid.jar your/java/Code.java
25 $ java -classpath .:path/to/simgrid.jar your.java.Code the/parameter/to/your/code
26 ~~~~
27
28 For example:
29 ~~~~{.sh}
30 $ cd examples/java
31 $ java -classpath ../../simgrid.jar:. .:../../simgrid.jar app.pingpong.Main ../platforms/platform.xml 
32 ~~~~
33
34 Any SimGrid simulation (java or not) is usually constituted of several
35 kind of actors or processes (classes extending @c Msg.Process) that
36 are deployed over the hosts of the virtual platform. So, your code
37 should declare these actors, plus a Main class in charge of deploying
38 your actors on the platform. Please refer to the examples for details.
39
40 @subsection java_use_trouble Troubleshooting
41
42 Actually, these bindings are not only implemented in Java. They do use
43 the C implementation of SimGrid. This should be transparent as this
44 library is directly included in the `simgrid.jar` file but things can
45 still go wrong is several ways.
46
47 ** **Error: library simgrid not found**
48
49 This means that the JVM fails to load the native library. You should
50 try to rebuild the simgrid.jar file as explained above. If it does not
51 help, you can try to use an installed version of the library instead
52 of the one included in the jar. For that, specify the path to the
53 native library in the `LD_LIBRARY_PATH` variable (or in the
54 `DYLD_LIBRARY_PATH` on Mac OSX).
55
56 ~~~~{.sh}
57 $ export SIMGRID_ROOT="$HOME/Install/simgrid/" # change it to the path where you installed the SimGrid library
58 $ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$SIMGRID_ROOT/lib
59 ~~~~
60
61 Add these lines to your `~/.bashrc` file or equivalent to make these
62 settings permanent even after a reboot.
63
64 ** **Other errors**
65
66 When using jMSG, your program can crash for 3 main reasons:
67 - Your Java part is not good: you'll have a good old java exception thrown,
68   and hence you should be able to correct it by yourself.
69 - Our java part is not good: you'll also have a java exception thrown, but
70   we have real doubts this can happen, since the java part is only a JNI
71   binding. The other option is that it crashed because you used incorrectly
72   the MSG API, so this means also you should have an MSGException. It means
73   you should read carefully MSG samples and/or documentation.
74 - Something has crashed in the C part. Okay, here comes the tricky
75   thing. It happens mainly for 2 reasons:
76   - When something goes wrong in your simulation, sometimes the C part stops
77     because you used SimGrid incorrectly, and JNI bindings are not fond of that.
78     It means that you'll have something that looks ugly, but you should be able
79     to identify what's going wrong in your code by carefully reading the whole
80     error message
81   - It may happen that the problem comes directly from SimGrid: in this case,
82     the error should be uglier. In that case, you may submit a bug directly to
83     SimGrid.
84
85 @section java_coroutines Java bindings on Steroids
86
87 <b>THIS SECTION IS SOMEWHAT OBSOLETE</b> because building a JVM
88 providing coroutines is probably not possible anymore nowadays. If you
89 really need performance for your Java simulation, please contact us.
90
91 There is two main motivations to use the coroutine variant of SimGrid
92 Java bindings: it's about 5 times faster than the default thread-based
93 context factory, and the amount of runnable processes is then only
94 limited by the amount of RAM that you have. The drawbacks are that it
95 requires a specific and rather experimental JVM to run, and that this
96 context factory itself remains a bit experimental so far.
97
98 @subsection  bindings_java_coro_install Getting a mlvm JVM
99
100 You need to get a patched JVM from [here](http://ssw.jku.at/General/Staff/LS/coro/)
101 (many thanks to Lukas Stadler for this work!).
102
103 You can either get a prebuilt binary, or recompile your own JVM. Make
104 sure to get a coro-simple version, as we don't need to serialize nor
105 migrate stacks in SimGrid. You should be able to follow the `README.txt`
106 that you'll get in the repository, but here is how we did it, just in
107 case. The instructions are given for a debian or Ubuntu box, but I
108 think you should manage to convert it to your system quite easily.
109 Finally, if you're really stuck, you can get the version compiled by
110 Jonathan Rouzaud-Cornabas from his web page. This version is known to
111 work with SimGrid for sure!
112 http://graal.ens-lyon.fr/~jrouzaud/files/corosimple-linux-amd64-20120914.tgz
113
114  -# Install mercurial and some dependencies
115 ~~~~{.sh}
116 sudo apt-get install mercurial ksh libfreetype6-dev libcups2-dev libasound2-dev gawk openjdk-7-jdk libxext-dev libxrender-dev libxtst-dev
117 # Grab the forest extension: we need to source-install it
118 hg clone https://bitbucket.org/gxti/hgforest hgforest
119 ~~~~
120  -# Configure the mercurial extensions: Edit ~/.hgrc and paste the
121     following lines. Don't forget to change the /path/to/forest.py to
122     point to where you just downloaded the source.
123
124     Forest extension is needed to download the openjdk source code and
125     patches while the mq line is needed to apply the patches. The
126     username is needed at the step "preparing the sources", not sure why.
127 ~~~~
128 [ui]
129 username = YouUserameWithoutSpaces
130 [extensions]
131 forest=/path/to/forest.py
132 mq=
133 ~~~~
134  -# Prepare the source code
135 ~~~~{.sh}
136 # create a working directory, and enter it
137 mkdir davinci; cd davinci
138
139 # Grab the sources
140 hg fclone http://hg.openjdk.java.net/hsx/hotspot-comp sources
141 # Grab the patches
142 hg fclone http://hg.openjdk.java.net/mlvm/mlvm patches
143
144 # Link the patch directories into the sources
145 bash patches/make/link-patch-dirs.sh sources patches
146 # Test wether the previous command worked with
147 ls -i patches/hotspot/series sources/hotspot/.hg/patches/series
148 # It should display something like the following.
149 # (note that both file share the same inode number)
150 #  9707849 patches/hotspot/series
151 #  9707849 sources/hotspot/.hg/patches/series
152
153 # Specify what to compile.
154 export davinci=${pwd} guards="buildable testable coro-simple"
155 # Apply the patches
156 sh patches/make/each-patch-repo.sh hg qselect --reapply $guards `sh $davinci/patches/make/current-release.sh`
157 # Check that it understood that you want the patch applied:
158 grep -r GLOBAL_GUARDS patches/make/
159 # this should display something like the following (maybe amonst other unrelated lines)
160 # GLOBAL_GUARDS=buildable testable coro-simple
161 # If this does not work, edit patches/make/Makefile,
162 #   manually coro-simple to GLOBAL_GUARDS and then
163 #   rerun the patches/make/each-patch-repo.sh script as earlier
164
165
166 # Finish the setup
167 cd patches/make;
168 make setup && make force && make && make FORCE_VERSIONS=1 && echo "Sources are properly setup"
169 # If this last command failed, check your mercurial config within ~/.hgrc (see above)
170 ~~~~
171  -# Compile it all
172 ~~~~{.sh}
173 unset LD_LIBRARY_PATH
174 export ALT_BOOTDIR=/usr/lib/jvm/java-7-openjdk-amd64/
175 cd sources
176 # Check that everything is fine
177 make sanity
178 # Go for it (it takes about half an hour on my machine)
179 make all
180
181 # Check that the coroutine library got compiled in
182 ls sources/build/linux-amd64/classes/java/dyn/
183 # This should display a bunch of class files. If not, something went wrong, you need to investigate further
184 ~~~~
185
186 @subsection  bindings_java_coro_use Using coroutine contexts
187
188 SimGrid Java will automatically switch to the coroutine context
189 factory if your JVM support it, so you will just need to execute your
190 simulation with the correct JVM. The selected context factory gets
191 displayed automatically.
192 ~~~~{.sh}
193 export LD_LIBRARY_PATH=/path/to/simgrid.so:/path/to/libsimgrid-java.so
194 cd examples
195 $PATH_TO_COROUTINE_JVM/java -classpath .:../simgrid.jar masterslave.Masterslave masterslave/ masterslaveDeployment.xml platform.xml
196 ~~~~
197
198 Note that you may have to adjust the "coro.stacksPerThread"
199 configuration option to run large simulations. The default is 100 and
200 you want to increase it to run more processes.
201 ~~~~{.sh}
202 $ $PATH_TO_COROUTINE_JVM/java -Dcoro.stacksPerThread=$STACKS_NUMBER -classpath .:../simgrid.jar basic/BasicTest platform.xml basic/basicDeployment.xml
203 ~~~~
204
205 If you reach the point where the creation of new simulated processes
206 fail with the message "Can't create coroutine object", you may need to
207 increase the relevant system limit with the following command.
208 ~~~~{.sh}
209 sysctl -w vm.max_map_count = 131072
210 ~~~~
211
212 The full story is that each coroutine requires two memory maps, and
213 that Linux puts a limit on the total amount of memory maps that each
214 process can manage (by default, this limit is often at 65535). Since
215 the JVM needs a few dozen of such maps on its own (three maps per
216 dynamic library -- check `/proc/the_pid/maps` if you don't believe it),
217 this is enough to create over 30,000 simulated processes. But to go
218 futher, that limit must be modified.
219
220 If you want to make this change permanent on your machine, edit your
221 `/etc/sysctl.conf` file. Otherwise, you have to redo it by calling
222 sysctl after each reboot.
223
224  */