Real-time computing is often associated with high speed, but this is only one part of the picture. At its core, real-time computing is about predictability -- the knowledge that the system will always perform within the required time frame. The deadlines involved need not be very short -- though they sometimes are -- and the consequences of missing a deadline may not be dire -- though they sometimes are. The key to whether an application is a real-time one has to do with whether its requirements include temporal constraints. The developers of the Real-Time Specification for Java (RTSJ), JSR 1, use this definition of real-time computing: The programming environment must provide abstractions necessary to allow developers to correctly reason about the temporal behavior of application logic. It is not necessarily fast, small, or exclusively for industrial control; it is all about the predictability of the execution of application logic with respect to time. Real-time applications are partitioned into a set of tasks, some of which have deadlines. The goal is for all tasks to be scheduled in such a way as to complete before their deadline, if any. Definitions: Hard-Real-Time and Soft-Real-Time
Real-time systems may be classified differently depending on the system's requirements. A hard-real-time system is one in which the system must meet all deadlines without fail. Typically, such systems also have short latencies, the time between when a triggering event occurs and when the response must begin or complete, often measured in microseconds or milliseconds. Many hard-real-time systems are further classified as safety-critical systems. These systems are used to protect humans from injury or danger. Safety-critical systems must go through exhaustive testing and a line-by-line code review before certification. Note that safety-critical Java technology is being studied through JSR 302 and is not addressed by the RTSJ at the time of this writing. A soft-real-time system, on the other hand, is one that will still function correctly, according to its specification, if the system occasionally misses deadlines. For example, a cellular telephone switching system is a soft-real-time system: Customers expect all calls to be completed, but an occasional deadline miss may cause an acceptable failure, such as audio dropouts or delayed call setup. Soft-real-time systems typically specify what percentage of deadlines can be missed or how often this is acceptable. Unpredictability in a Java Technology-Based Application
A number of factors may render the timing of execution unpredictable and therefore may cause a standard Java task to miss its deadline. Here are the most common.
The Real-Time Specification for Java (RTSJ)
The Real-Time Specification for Java (RTSJ), or JSR 1, specifies how Java systems should behave in a real-time context and was developed over several years by experts from both the Java and real-time domains. The RTSJ is designed to seamlessly extend any Java family -- whether the Java Platform, Standard Edition (Java SE); Java Platform, Micro Edition (Java ME); or Java Platform, Enterprise Edition (Java EE) -- and has the requirement that any implementation pass both the JSR 1 technology compatibility kit (TCK) and the TCK of the platform -- Java SE, Java ME, or Java EE -- on which it is based. The RTSJ introduces several new features to support real-time operations. These features include new thread types, new memory-management models, and other newly introduced frameworks. Let's start with how the RTSJ views tasks and time. Tasks and Deadlines
The RTSJ models the real-time part of an application as a set of tasks, each with an optional deadline. The deadline of a task is when the task must be completed. Real-time tasks fall into several types, based on how well the developer can predict their frequency and timing:
The RTSJ uses this task-type information in several ways to ensure that critical tasks do not miss their deadlines. First, the RTSJ allows you to associate with each task a deadline miss handler. If a task does not complete before its deadline, this handler is called. Deadline-miss information can be used in deployment to take corrective action or to report performance or behavioral information to the user or to a management application. By comparison, in non-real-time applications, failures may not become apparent until secondary or tertiary side effects arise -- such as request timeouts or memory depletion -- at which point it may be too late to recover gracefully. Deadline-miss handling is deferred to a deadline miss handler, like this:
Or if there is no handler, it can be performed by the thread itself:
Thread Priorities
In a true real-time environment, thread priorities are extremely important. No system can guarantee that all tasks will complete on time. However, a real-time system can guarantee that if some tasks are going to miss their deadlines, the lower-priority tasks are victimized first. The RTSJ defines at least 28 levels of priority and requires their strict enforcement. However, as this article mentioned earlier, RTSJ implementations rely on a real-time operating system to support multiple priorities, as well as on the ability of higher-priority threads to preempt lower-priority ones. The problem of priority inversion can undermine the effectiveness of a priority facility. Accordingly, the RTSJ requires priority inheritance in its scheduling. Priority inheritance avoids priority inversion by boosting the priority of a thread that is holding a lock to that of the highest-priority thread waiting for that lock. This prevents a higher-priority thread from being starved because a lower-priority thread has the lock that it needs but cannot get adequate CPU cycles to finish its work and release the lock. This feature also prevents a medium-priority task that does not depend on the shared resource from preempting the higher-priority task. In addition, the RTSJ is designed to allow both non-real-time and
real-time activities to coexist within a single Java application. The
degree of temporal guarantees provided to an activity depends on the
type of thread in which the activity is executing:
One of the problems with automatic memory management in standard virtual machines (VMs) is that one activity may have to "pay for" the memory-management costs of another activity. Consider an application with two threads: a high-priority thread (H) that does a small amount of allocation, and a low-priority thread (L) that does a great deal of allocation. If H is unlucky enough to run at a time when L has consumed almost all the available memory in the heap, the garbage collector may kick in and run for a long time when H goes to allocate a small object. Now, H is paying -- in the form of an incommensurately long delay -- for L's enormous memory consumption. The RTSJ provides a subclass of RTT called To maximize predictability, NHRTs are allowed neither to use the garbage-collected heap nor to manipulate references to the heap. Otherwise, the thread would be subject to GC pauses, and this could cause the task to miss its deadline. Instead, NHRTs can use the scoped memory and immortal memory features to allocate memory on a more predictable basis. The following two NHRT examples demonstrate this. NHRT -- Example 1
NHRT -- Example 2
Memory Areas
The RTSJ provides for several means of allocating objects, depending on the nature of the task doing the allocation. Objects can be allocated from a specific memory area, and different memory areas have different GC characteristics and allocation limits.
For each thread, there is always an active-memory area, called the current allocation context. All objects allocated by a thread are allocated from this area. The current allocation context changes when you execute a block of code with a specific memory area. The memory area API contains an Advanced Communication Between Threads
One of the advantages of the RTSJ is that it allows real-time and non-real-time activities to coexist within a single VM. However, communication between threads involves memory, and this poses a challenge. The obvious mechanism for communication between threads is a queue, where one thread is putting data onto the queue and the other thread is removing data from the queue. Imagine that an RTT is putting data onto a queue every 10 milliseconds, and a non-RTT is consuming the data from the queue. What happens if the non-RTT does not get enough CPU time to drain the queue? The queue will grow, and you have a choice of three bad options:
The first option is impractical, and the last option is unacceptable. The predictability of real-time activities should not be affected by the behavior of lower-priority activities. The RTSJ supports several types of non-blocking queues for communicating between threads. When they are used between real-time and non-RTTs, there are some restrictions on the memory area in which elements must reside. Part 2 of this series will examine the issue of real-time garbage collection and the benefits of the Sun Java Real-Time System. _______
Acknowledgments
The editor of this article wishes to thank David Holmes and Antonia Lewis for their work on the white paper on which this article is based. And thanks to Carlos Lucasius for his valuable technical review comments. For More Information
Real-Time Specification for Java (RTSJ), JSR 1
Brian Goetz, a senior staff engineer at Sun, has been a professional software developer for 20 years. He is the author of over 75 articles on software development, and his book, Java Concurrency in Practice, was published in May 2006 by Addison-Wesley. He serves on the JCP Expert Groups for JSRs 166, concurrency utilities; 107, caching; and 305, annotations for safety analysis. Robert Eckstein has worked with Java technology since its first release. In a previous life, he has been a programmer and editor for O'Reilly Media, Inc. He has written or edited a number of books, including Java Swing, Java Enterprise Best Practices, Using Samba, XML Pocket Reference, and Webmaster in a Nutshell. | ||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||