| By Paul Hohensee | Article Rating: |
|
| March 2, 2009 08:35 AM EST | Reads: |
10,635 |
// self is a Thread // self is a RealtimeThread
while (true) do { self.setPriority(myRT_Priority);
compute_data(); self.setSchedulingParameters(myPeriod);
now = System.currentTimeMillis(); while (true) do {
Thread.sleep(next_period – now); compute_data();
send_data(); Thread.waitForNextPeriod();
next_period += period; send_data();
} }
Further programming tips and code examples can be found at http://java.sun.com/javase/technologies/realtime/.
Key Concepts
Before discussing predictability, we must first define it, along with a few related terms.
A program is predictable if its jitter, i.e., the variation in its total response time, is bounded. A program is deterministic to the extent that its jitter approaches zero; a perfectly deterministic program has a jitter of zero. We can’t attain that, but it doesn’t matter in practice as long as jitter can be bounded. For a financial application, jitter might be defined in terms of transaction time. For an industrial control application, it might be defined in terms of the length of time it takes to figure out the next position of a robotic arm.
A deadline is the time by which a program must complete a response. In a soft real-time application, missing one or a few deadlines isn’t critical, but in a hard real-time application, no deadline can be missed.
In a multi-threaded program, usually only a few threads execute code that must be predictable. We call such threads critical. We refer to the rest of the threads as non-critical. In a real-time system, critical threads always execute at a critical real-time priority above that of non-critical threads. Real-time priorities must be enforced by the operating system and are always higher than normal time-sharing thread priorities. The priorities of normal threads are in practice advisory, since non-real-time threads can be preempted by the operating system at any time. Threads with real-time priorities run until blocked, which means that if used carelessly they can potentially hang the system.
When running a Java software program, jitter can be caused by many system activities, especially internal virtual machine activities. The common sources of jitter include class loading and static initialization, dynamic compilation, and the execution of compiled code and garbage collection. Jitter in each of these categories can be bounded, but only at the price of program throughput. Thus we recognize a predictability spectrum, with high throughput and low predictability at one end and lower throughput and high predictability at the other. You can usually configure a conventional virtual machine, like the HotSpot JVM Sun Microsystems ships in the Java Platform Standard Edition (Java SE), to achieve soft real-time predictability down to about 20 milliseconds or so on multi-processor systems. However, to go much lower, and especially to get down below a millisecond, requires a real-time virtual machine such as that included in Java RTS.
In a virtual machine like the HotSpot JVM, the primary performance criterion is the average execution time of an application, which translates to maximum throughput. Certain, infrequently used, virtual machine operations can be allowed to execute in an unpredictable amount of time if doing so allows the most common cases and more frequent operations to execute faster. This is great if throughput is your only objective, but not so great if you need predictability.
Class Loading
A virtual machine normally loads a class when a program first tries to create an instance of that class, or when a program tries to use a static method or variable of that class. When a class is loaded, its static variables are initialized. This activity takes time and can interfere with the execution of any code that is critical for the real-time application. Since it isn’t possible to predict when classes will be loaded or how long it will take, it’s also not possible to predict when this jitter will occur, nor whether the amount of the jitter is acceptable. In both real-time and non-real-time virtual machines, you can avoid this problem by referencing and using all classes utilized by critical threads before they start executing time-critical code. In addition, Java RTS allows you to specify a list of classes to be pre-loaded and initialized at start-up.
Synchronization
High-performance virtual machines, like the HotSpot JVM, heavily optimize the common case of uncontended object locking, typically using branch instructions first, then atomic instructions, and, only as a last resort, an operating system mutex when contention occurs. Java RTS can’t use branch instructions in the uncontended case because that mechanism requires a stop-the-world pause, during which no application thread can run. It does, however, use atomic instructions in preference to mutexes as long as there’s no real contention. In addition, Java RTS provides a mechanism to force allocation of a mutex for a particular object so that the associated OS-induced jitter doesn’t happen while executing time-critical code.
Compilation
One of the most common sources of jitter is run-time compilation. Since compiled code executes faster than interpreted code, a program can run faster if frequently executed pieces of code are compiled during program execution. If, however, compilation interrupts the execution of a section of time-critical code, determinism will be seriously compromised.
Another source of unpredictable compilation-related jitter is lazy, or on-demand, resolution of symbols (fields, classes and methods). When an unresolved symbol is encountered during the execution of generated code, a virtual machine can use some extra, non-deterministic code to resolve the symbol. In this way, a virtual machine can avoid resolving symbols that are never actually used and improve average performance. However, since the timing of the resolution operation itself is unpredictable, lazy symbol resolution is unacceptable in time-critical code.
Another issue is that optimized code typically makes assumptions about runtime conditions that, if violated, must result in abandoning the generated code and reverting back to either an interpreter or to completely unoptimized code. This process is called deoptimization and requires a stop-the-world pause. Time-critical code cannot depend on this sort of optimization and must be compiled to run more slowly than non-time-critical code.
Virtual machines, like the HotSpot JVM, make no attempt to control compilation-induced jitter other than to compile in the background on multi-processor systems. Java RTS, on the other hand, currently supports two compilation modes: Just-In-Time Compilation (JIT) and Initialization Time Compilation (ITC).
Published March 2, 2009 Reads 10,635
Copyright © 2009 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Paul Hohensee
Paul Hohensee is a senior staff engineer at Sun Microsystems where he is the Java Platform Standard Edition JVM Technical Lead. His primary focus is JVM performance along with hardware and platform support for Java and other dynamic languages. Earlier in his career he worked on dynamic binary translation as well as optimizing compilers and libraries for the first symmetric multiprocessors.
- Kindle 2 vs Nook
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX





































