Introduction What’s Java? Java vs C/C++ From C to Java JavaTM & Real-Time Advantages, Limitations, & RTSJ Java Threads Thread Example Interrupts Christos K LOUKINAS Monitors Timers Christos.Kloukinas@imag.fr JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Verimag (http://www-verimag.imag.fr/) Grenoble, France Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.1/56 Introduction Introduction What’s Java? Java vs C/C++ Java is a language with lots of merits We will examine: Its advantages for Real-Time programming; From C to Java Java Threads Thread Example Its disadvantages for Real-Time programming; and, Interrupts Monitors Timers The RTSJ proposal. JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.2/56 What’s Java? Java is: Introduction Object-Oriented; What’s Java? Java vs C/C++ From C to Java Type-Safe; Java Threads Thread Example Interrupts Portable; Monitors Timers JNI Library-Rich; and, CNI Disadvantages Speed has automatic Garbage Collection. JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.3/56 Java versus C/C++ O-O helps with structuring code and decreasing code dependencies. Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Increased type-safety, bound-checks and lack of pointer-arithmetic increase overall safety and allow for aggressive compiler optimisations. Monitors Timers JNI CNI Disadvantages Built-in exceptions, threads & synchronisation means that the compiler knows how to do “The Right Thing”. Speed JIT or AOT? Real-Time GC RTSJ Scheduling Built-in GC means no more dangling pointers & a lot fewer memory leaks. Memory Synchronisation Asynchronous Other Proposals Why Java? Fixed size and byte order of basic types helps with porting your software. Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.4/56 From C to Java Structures now become Classes and have associated Methods. Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Easier to tell what functions can have access to a structure. No need to check the kind of structure another function passed you - the O-O internal mechanisms do this automatically. Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory O-O: How to let your compiler write your switches. . . Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.5/56 From C to Java - II Controlling access to methods, fields & classes themselves: Introduction What’s Java? public Java vs C/C++ From C to Java private Java Threads Thread Example Interrupts protected Monitors Timers JNI CNI Disadvantages packages (only one class can be public in a package) Speed JIT or AOT? Real-Time GC RTSJ Scheduling Other modifiers: abstract / static / final / strictfp Memory Synchronisation Asynchronous synchronized (equivalent to a synchronized(this)) Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.6/56 From C to Java - III Introduction What’s Java? Java vs C/C++ unions & enums don’t exist - use polymorphism (or compile time constants if it’s not worth the abstraction) From C to Java Java Threads Thread Example multiple inheritance - use interfaces or composition Interrupts Monitors Timers JNI Reflection - getClass, getMethods, getPackage, isInstance CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.7/56 From C to Java - III Introduction What’s Java? Java vs C/C++ unions & enums don’t exist - use polymorphism (or compile time constants if it’s not worth the abstraction) From C to Java Java Threads Thread Example multiple inheritance - use interfaces or composition Interrupts Monitors Timers JNI Reflection - getClass, getMethods, getPackage, isInstance CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Unlike C++, there are no object destructors! Cannot control when finalizers will be executed (if they ever are. . . ) Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.7/56 Concurrent Programming with Java Java has: Introduction Threads What’s Java? Java vs C/C++ From C to Java Monitors (& Condition Variables) Java Threads Thread Example Interrupts Monitors Timers Timers JNI JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.8/56 Threads in Java Introduction What’s Java? Threads implement the Runnable interface, i.e., provide a run method Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Threads have priorities: from MIN_PRIORITY to MAX_PRIORITY, default is NORM_PRIORITY Timers JNI CNI Threads have lower priority than the GC (always) Disadvantages Speed JIT or AOT? Resource allocation is not (necessarily) priority-based Real-Time GC RTSJ Scheduling Memory Synchronisation Stack size can be declared, but it can also be ignored. . . Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.9/56 Threads in Java - II Dæmon threads (cannot keep an application running) Introduction What’s Java? Java vs C/C++ From C to Java Have methods: holdsLock, interrupt, join, yield, sleep, destroy (not implemented) Java Threads Thread Example Interrupts No longer have: resume, stop, suspend Monitors Timers JNI Interrupts: CNI Disadvantages Speed JIT or AOT? Real-Time GC Threads poll for interrupts with one of: wait, sleep, join, isInterrupted RTSJ Scheduling Memory The target thread can poll whenever it wants Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.10/56 Starting a Thread Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS class ERR { static public void pr(String msg){ System.err.println(Thread. currentThread().toString()+": "+msg) ;}} class MyThread extends Thread { Thread parent; public MyThread() {parent = Thread. currentThread();} public void run() { ERR.pr("Hello"); } } École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.11/56 Starting a Thread - II Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals class Main { public static void main(String[] argv) { MyThread aThread = new MyThread(); ERR.pr("this is main"); aThread.start(); // calls run() try {aThread.join();} catch (InterruptedException ie){ERR.pr ("Un-joinable"+ie);} finally {ERR.pr("Bye");} } } Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.12/56 Interrupting a Thread Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous class MyThread extends Thread { // As before ... public void run() { ERR.pr("Hello"); try {this.sleep(2);} catch ( InterruptedException ie) {} parent.interrupt(); } } Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.13/56 Interrupting a Thread - II Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory class Main { public static void main(String[] argv) throws InterruptedException { MyThread aThread = new MyThread(Thread .currentThread()); ERR.pr("this is main"); aThread.start(); // calls run() try { aThread.join(); Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.14/56 Interrupting a Thread - III if (Thread.currentThread(). isInterrupted()) { ERR.pr("Was interrupted"); throw new InterruptedException(" Suicide...");} } catch (InterruptedException ie) { ERR.pr("Un-joinable:" + ie); throw ie; } finally {ERR.pr("Bye");} ERR.pr("Never printed..."); Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous } Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS } École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.15/56 Monitors Each Java object has its own semaphore and its own condition variable. Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts We use the synchronized(object){} construct to denote a critical region block protected by a monitor on the semaphore of the object. Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC Inside a monitor, we use wait() & notify() to wait on a condition & to flag it. Can also use wait(/*timeout*/) and notifyAll(). RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? A break, continue, return, throw inside a monitor will exit it after having unlocked the object. Are you sure you do so in C/C++? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.16/56 Monitors - II Entering a monitor is not priority-based. Introduction What’s Java? Java vs C/C++ From C to Java A timed wait can wait for more than the timeout specified. Java Threads Thread Example Interrupts Monitors Timers Choosing a thread to notify is not priority-based. No priority-sorted wait queues. JNI CNI Disadvantages No priority-inversion avoidance protocol. Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.17/56 Timers Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? java.lang.Timer & java.util.TimerTask One-time execution; Repeated execution. fixed-delay nth execution starts period milliseconds after the (n − 1)th execution (delays are therefore additive) fixed-rate nth execution starts n∗period milliseconds after the first execution (try to keep a constant rate - may execute many times in rapid succession to catch up) Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.18/56 Timers - II No Real-Time guarantees! – Using wait(/*timeout*/) Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors If a timer task takes too long, it can delay future executions. Not a dæmon thread by default (can keep an application from terminating) Timers JNI CNI Disadvantages Speed When should I run if no delays? scheduledExecutionTime() JIT or AOT? Real-Time GC RTSJ A timer can be associated with many timer tasks Scheduling Memory Synchronisation A timer task can be associated with many timers Asynchronous Other Proposals Why Java? cancel() a timer or a timer task Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.19/56 Timer Example Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals class MyTimerTask extends TimerTask { private int MAX_TARDINESS = 15; public void run() { ERR.pr(toString()+": Started"); if (System.currentTimeMillis() scheduledExecutionTime() >= MAX_TARDINESS) return; // Too late ERR.pr(toString()+": running @ " + System.currentTimeMillis()); } Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.20/56 Timer Example - II public static void main(String[] argv) { MyTimerTask aTimerTask = new MyTimerTask(); Timer aTimer = new Timer(false); // Not a daemon timer // Start=10 ms, period=30 ms aTimer.scheduleAtFixedRate(aTimerTask , 10L, 20L); // aTimer.cancel(); } Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals } Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.21/56 Native Interface Example - Java Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals class HelloWorld { private native void print(); static { System.loadLibrary("HelloWorld"); } public static void main(String[] args) { Thread.currentThread().setName("The Main from Java"); new HelloWorld().print(); } } Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.22/56 Native Interface Declaring methods as native Introduction What’s Java? Loading them up at start-up using static blocks Java vs C/C++ From C to Java Java Threads Finding out signatures using javap -s class Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.23/56 Type Signatures Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors void boolean byte int float V Z B I F char short long double C S J D Object of class X : LX; Array of type X : [X ret foo(args) : (args)ret Timers JNI CNI Disadvantages Speed JIT or AOT? String[] foo(Thread[] arr, int i, long j): ? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.24/56 Type Signatures Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors void boolean byte int float V Z B I F char short long double C S J D Object of class X : LX; Array of type X : [X ret foo(args) : (args)ret Timers JNI CNI String[] foo(Thread[] arr, int i, long j): Disadvantages Speed JIT or AOT? ([Ljava/lang/Thread;IJ)[Ljava/lang/String; Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.24/56 JNI Can do lock/unlock, new, etc. Introduction What’s Java? It’s heavy - all queries use strings Java vs C/C++ From C to Java Java Threads Cannot optimise code written with it Thread Example Interrupts Monitors Java threads are not necessarily system threads Timers JNI CNI Disadvantages Speed May have to use a finalizer It will be run before the memory is reused. . . JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.25/56 CNI Used with GCJ (front-end of GCC) Introduction What’s Java? Java vs C/C++ Java classes are C++ classes (not vice versa) From C to Java Java Threads Thread Example Faster & can be optimised Interrupts Monitors Timers A lot simpler to write code JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.26/56 Disadvantages Java is slow; Introduction What’s Java? Java’s memory model is convoluted (Under revision); Java vs C/C++ From C to Java Java Threads The GC is unpredictable; Thread Example Interrupts Monitors Scheduling is unpredictable; Timers JNI CNI Monitors can suffer from priority-inversion; Disadvantages Speed JIT or AOT? Notification is not priority-based; Real-Time GC RTSJ Scheduling Timers are not accurate; Memory Synchronisation Asynchronous Other Proposals Why Java? Design for portability ⇒ Cannot use any special OS mechanisms Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.27/56 Speed It’s not Java, it’s the implementations; Introduction What’s Java? Java vs C/C++ One can use a JIT or an AOT compiler; From C to Java Java Threads Thread Example Interrupts Monitors It’s easier to optimise Java than C/C++ No (wild) casts, pointer-arithmetic, etc.. Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.28/56 JIT or AOT? Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JIT: can do anything a JVM does; works on bytecodes; needs resources; cannot optimise much; is unpredictable. JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals AOT: can do aggressive optimisations; no need for extra resources at the target system; is predictable; can always add a JVM for loading classes. Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.29/56 Real-Time GC Interruptible, low latency, Real-Time GC’s exist Introduction What’s Java? Java vs C/C++ Usually meant for large or non-critical systems From C to Java Java Threads Thread Example But there is also the memory region technique. Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.30/56 RTSJ Thread Scheduling & Dispatching Introduction What’s Java? Java vs C/C++ Memory Management From C to Java Java Threads Thread Example Physical Memory Access Interrupts Monitors Timers Synchronisation & Resource Sharing JNI CNI Disadvantages Asynchronous Event Handling Speed JIT or AOT? Real-Time GC Asynchronous Transfer of Control RTSJ Scheduling Memory Asynchronous Thread Termination Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.31/56 Scheduling Preemptive, Priority-based by default At least 28 different priorities Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals RealtimeThread Can access the heap Lower priority than the GC NoHeapRealtimeThread No heap accesses Higher priority than the GC AsyncEventHandler Depends on the noheap arg Can be unbound or bound to a particular thread It can block (holding resources) Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.32/56 Scheduling - II Need to have a reference to a Scheduler obj. Introduction What’s Java? Java vs C/C++ From C to Java A Scheduler has params for priority, importance, memory, etc. Java Threads Thread Example Interrupts Monitors PeriodicParameters, AperiodicParameters, SporadicParameters Timers JNI CNI Disadvantages Speed Processing group to treat non-periodic threads as a single periodic one JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Max. Execution time, Deadline & overrun handlers The execution overrun handler may be ignored & use the duration only as a hint to the feasibility algorithm. Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.33/56 Scheduling - III Introduction What’s Java? Java vs C/C++ Scheduler.addToFeasibility(Schedulable) to add another task in the feasibility analysis performed by the scheduler. From C to Java Java Threads Thread Example Interrupts Scheduler.isFeasible() to find out if the current system can be indeed scheduled Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Changing the parameters of a task at run-time through the setIfFeasible(Schedulable, ReleaseParameters, MemoryParameters) Memory Synchronisation Asynchronous Other Proposals waitForNextPeriod() for periodic tasks. Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.34/56 Scedulable Objects Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI setScheduler( Scheduler sched, SchedulingParameters sched_params, ReleaseParameters release_params, MemoryParameters mem_params, ProcessingGroupParameters group) Disadvantages Speed JIT or AOT? Real-Time GC RTSJ defines the PriorityScheduler, with two RTSJ Scheduling Memory final int fields, MAX_PRIORITY and MIN_PRIORITY. Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.35/56 Scheduling Parameters RTSJ defines two kinds of Scheduling parameters: Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI PriorityParameters(int priority) and an additional one for declaring the importance of the threads along with their priority ImportanceParameters(int priority, int importance) CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.36/56 Release Parameters - Periodic Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory PeriodicParameters( HighResolutionTime start, /* null = start when released */ RelativeTime period, RelativeTime cost, RelativeTime deadline, /* use period when null */ AsyncEventHandler overrunHandler, AsyncEventHandler missHandler) Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.37/56 Release Parameters - Aperiodic Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers AperiodicParameters( RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler) JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.38/56 Release Parameters - Sporadic Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI SporadicParameters( RelativeTime minInterarrival, RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler) Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.39/56 Memory Parameters Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ MemoryParameters( long maxMemoryArea, // or MemoryParameters.NO_MAX (units= bytes) long maxImmortal, // or NO_MAX (units=bytes) long allocationRate) // or NO_MAX (units=bytes/sec) Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.40/56 Thread Group Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? ProcessingGroupParameters( HighResolutionTime start, RelativeTime period, RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler) Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.41/56 Creating a R-T Thread Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Scheduler sched= new PriorityScheduler(); PeriodicParameters pp = new PeriodicParameters( new RelativeTime(0,0),//when released new RelativeTime(100, 10), // period new RelativeTime(30, 0), // cost new RelativeTime(60, 0), // deadline null, // no Overrun Handler null); // no Miss Period Handler ImportanceParameters prio = new ImportanceParameters(MAX_PRIORITY, 3); MemoryArea ma = new LTMemory(1024, 1024); Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.42/56 Creating a R-T Thread - II Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? MemoryParameters mp = new MemoryParameters(MemoryParameters. NO_MAX,MemoryParameters.NO_MAX); ProcessingGroupParameters gp = null; RealtimeThread rt = new RealtimeThread(prio, pp, mp, ma, gp, new Runnable () {/*the run method...*/ }); rt.setScheduler(sched); if (!rt.getScheduler().isFeasible()) throw new Exception("Not feasible"); rt.start(); // Release the thread Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.43/56 Finding a New Scheduler Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS class SchedExample { public static Scheduler findSched(String policy) { String className = System.getProperty(" javax.realtime.scheduler."+policy); Class clazz; try { if (null != className && null != (clazz = Class.forName(className))) return (Scheduler) clazz.getMethod(" instance",null).invoke(null,null); } catch (/* lots of exceptions */) {} return null; } École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.44/56 Finding a New Scheduler - II Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Scheduler sched = findSched("EDF"); if (null != sched) { RealtimeThread rt = new RealtimeThread( /*...*/); rt.setScheduler(sched); if (!rt.getScheduler().isFeasible()) throw new Exception("Not feasible"); rt.start(); // Release the thread } Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.45/56 Memory Management Execution Stack, Heap & Memory Scopes Scopes give bounds to the lifetime of objects allocated within them Introduction What’s Java? Java vs C/C++ From C to Java Objects of a scope collected en masse after its Runnable exits Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ A scope is collected before being reused (not when exited, so finalizers still a problem) Hierarchy of scopes - inner, outer (or unrelated) Immortal memory : top-most scope, collected when the JVM exits Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS Objects surviving current scope need be allocated to the top-most scope which contains a reference to these objects École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.46/56 Memory Management - II Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts LTMemory versus VTMemory Allocation in LTMemory is linear with respect to object’s size. A VTMemory may be separately GC’ed and may be augmented if it’s not big enough Monitors Timers JNI Assignment Rules CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Heap Immortal Scoped Refs Heap Immortal Yes Yes Yes Yes Yes Yes Scoped No No Yes, if same or higher scope Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.47/56 Memory Management - III LTPhysicalMemory, VTPhysicalMemory, ImmortalPhysicalMemory Introduction What’s Java? SizeEstimator & reserve(class, instances) Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI RawMemoryAccess - Accessing a region interpreted as byte, short, int, long or as an array of such RawMemoryFloatAccess - Same but for float & double Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Maximum preemption latency of a Real-Time thread due to the GC is given by: RealtimeSystem.currentGC(). getPreemptionLatency() (returns a RelativeTime) Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.48/56 Synchronisation Priority inheritance protocol supported by default Introduction What’s Java? Java vs C/C++ Given the GC it’s impossible to implement PIP! From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages 3 Wait-free queues for avoiding this. WaitFreeWriteQueue (non-blocking write), WaitFreeReadQueue (non-blocking read), WaitFreeDequeue (non-blocking write & read) Speed JIT or AOT? Real-Time GC Real-Time part of queue never blocks - no need for PIP. RTSJ Scheduling Memory Synchronisation Asynchronous If action is not possible (e.g., queue is full) you decide (overwrite old value, ignore the new one) Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.49/56 Asynchronous Events Introduction What’s Java? AsyncEvent, AsyncEventHandler, handleAsyncEvent() ≡ run() Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers JNI CNI Disadvantages bindTo(String), addHandler(), fire() Data-less events Can have multiple handlers per event, events per handler Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Behave like RealtimeThread, or like NoHeapRealtimeThread if given a scoped region Can block POSIXSignalHandler if OS supports P OSIX Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.50/56 Asynchronous Transfer of Control Java’s longjmp Introduction What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors Timers A method must declare AsynchronouslyInterruptedException, otherwise it is postponed Don’t break code which doesn’t expect to be interrupted JNI CNI Disadvantages Speed JIT or AOT? Also postponed if in a synchronized block Don’t leave shared objects in an inconsistent state Real-Time GC RTSJ Scheduling Memory Synchronisation Use happened() to find out if the AIE you got is the one you expected Asynchronous Other Proposals Why Java? doInterruptible() if it is, propagate it if not Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.51/56 ATC - II Class Timed for Real-Time timers (throws an AIE) Introduction What’s Java? Timer restarted after doInterruptible() Java vs C/C++ From C to Java Java Threads resetTime() to use another duration Thread Example Interrupts Monitors Timers disable, enable, fire, propagate, isEnabled, happened JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Blocking java.io.* classes must know of AIE (unblock, raise IOException, or complete normally. . . ) Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.52/56 Asynch. Termination Allowing it everywhere, always is unsafe Introduction What’s Java? So we use ATC to do it Java vs C/C++ From C to Java Java Threads Thread Example Interrupts Monitors An AIE is treated as a normal exception, but a catch( ALL ) doesn’t catch it, unless we specifically test for the AIE with happened Timers JNI CNI Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.53/56 Other Proposals RTCE & HIP- from the J-Consortium.org Introduction What’s Java? Java vs C/C++ Ravenscar-Java- from the Ada community From C to Java Java Threads Thread Example JTRON- from Japan Interrupts Monitors Timers JNI CNI Java 2, Micro Edition (J2ME) - from SUN, for embedded systems Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Scheduling Memory Synchronisation Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.54/56 Why Java? Introduction What’s Java? Easier to write correct code (threads, monitors, exceptions) Java vs C/C++ From C to Java Java Threads Many libraries Thread Example Interrupts Monitors Strongly typed Timers JNI CNI No out-of-bound accesses, dangling pointers Disadvantages Speed JIT or AOT? Real-Time GC RTSJ Automatic GC- no more leaks Easier to have it optimised Scheduling Memory Synchronisation It has all the buzzwords. . . Asynchronous Other Proposals Why Java? Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.55/56 Why Java for Real-Time? Others are already using it Introduction Tools are getting better (and more) What’s Java? Java vs C/C++ From C to Java Java Threads Thread Example Using C/C++ is tedious, error-prone, difficult to maintain and to have it validated Interrupts Monitors Timers JNI Embedded systems is a huge market CNI Disadvantages Speed Software is replacing hardware JIT or AOT? Real-Time GC RTSJ Too complex for todays tools and languages Scheduling Memory Synchronisation Java promises to help us Asynchronous Other Proposals Why Java? Cannot fight a dream. . . So join it! Java for Real-Time? Christos K LOUKINAS École IN2P3 d’informatique: Temps Réel – 29 May 2003 – p.56/56