Sunday, February 3, 2008

Preparing for Real Time Java Development

To start development with Real time java you need suitable virtual machine and a real time operating system that meets criterias required by your virtual machine.
Specification of real-time java is defined through JSR 1 and JSR 282 (small improvements). As far as I know there are four compliant implementations.

  • Sun's Java RTS : Current version is 2.1. It supports real-time Linux on x86 and Solaris 10 on x86 and sparc. Although previous versions did not have a real-time garbage collector (a more predictable garbage collector), latest version has one. More information can be found at : http://java.sun.com/javase/technologies/realtime/
  • IBM's Websphere Realtime : Current version is 1.0. It only works only on real-time Linux. In addition to real-time java specification it contains a real-time garbage collector (metronome) and supports a head of time compilation. These two features are not required by real time java specification. More information can be found at : http://www.ibm.com/software/webservers/realtime/
  • Aonix' PERC : Has different versions Ultra, Pico and Raven. It has also a real-time garbage collector, and supports a head of time compilation. More information can be found at : http://www.aonix.com/perc.html
  • Timesys' RTSJ Reference Implementation : This is reference implementation and can not be used in production environment. But you can use it four practice. More information can be found at : http://www.timesys.com/java/

STEP 1 : Install Real Time Linux Kernel

I think the easiest way is to install ubuntu and later download a real-time extensions by following the instructions at http://wiki.ubuntu.com/RealTime. It is very simple, just execute apt-get just like installing a normal application. After that reboot your machine and select real-time kernel at startup from grub menu.


STEP 2 : Download and Install a Real Time JVM

You can obtain a 90 day fully working evaluation version of Sun' RTS 2.1 for Linux by registering with Sun's developer network. Just fill the Sun Java Real-Time System Survey form and a download link will be mailed to you. Althoug it is not supported on ubuntu, as far as I see it is working.
You may use other versions but I could not recomend working with Timesys reference implementation because it based on a very old virtual machine and does not support new classes and feature after java version 1.3. Sun's implemantation is fully compliant with JDK 5.0. Actually all RTSJ compliant implementations are required to pass test for JavaSE 5.0. That means you can run your JavaSE 5.0 applications as unmodified on a RTSJ compliant virtual machine by enjoying more determinism provided by real time garbage collector.


STEP 3: Prepare Eclipse

Open Window->Preferences and add Java RTS to Java->Installed JREs. Real time classes are in rt2.jar file in Sun's implementation. Right click on this file in eclipse and add javadoc located in RTS_INSTALLATION_FOLDER/doc/rtsj-docs as javadoc for this jar file.


STEP 4 : HelloRealTime

Following program will schedule a real time thread at every 100 miliseconds and put current time in an array allocated from immortal memory. At the end prints content of this array.

import javax.realtime.ImmortalMemory;
import javax.realtime.PeriodicParameters;
import javax.realtime.RealtimeThread;
import javax.realtime.RelativeTime;

public class HelloWorld extends RealtimeThread {
//allocate long array from immortal memory
public static long[] times = (long[]) ImmortalMemory.instance().newArray(long.class, 100);

public HelloWorld(PeriodicParameters pp) {
super(null, pp);
}

public void run() {
for (int i = 0; i < 100; i++) {
times[i] = System.currentTimeMillis();
waitForNextPeriod(); //wait for next period
}

}

public static void main(String[] argv) {
//schedule real time thread at every 100 milisecond
PeriodicParameters pp = new PeriodicParameters(new RelativeTime(100, 0));
HelloWorld rtt = new HelloWorld(pp);
rtt.start();

//wait real time thread to terminate
try {
rtt.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}

//print results
for (int i = 0; i < 100; i++) {
System.out.println(times[i]);
}
}
}

Resources