Real-Time Java * Programming Christopher D. Gill [email protected]Center for Distributed Object Computing Department of Computer Science Washington University, St. Louis http://www.cs.wustl.edu/~cdgill/RTSJ/ COOTS01_M4.ppt COOTS 2001 Tutorial M4 Monday, January 29, 2001 * Java TM is a registered trademark of Sun Microsystems
67
Embed
Real-Time Java * Programming Christopher D. Gill [email protected] Center for Distributed Object Computing Department of Computer Science Washington.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
*JavaTM is a registered trademark of Sun Microsystems
Christopher D. Gill
Tutorial Objectives• Provide an overview of real-time programming issues• Describe a motivating real-time programming example
– An on-line stock market analysis tool• Exhibits canonical requirements and issues
common to other classes of real-time systems• Show through incremental evolution of the example
– How real-time programming issues can arise in a JavaTM (Java) programming environment
– How features of the Real-Time Specification for JavaTM (RTSJ) can be applied to resolve these issues
Real-Time Java Programming
Christopher D. Gill
Example: Stock Market Analysis Tool• Performs automated decision aiding for stock trading• Inputs arrive from real-time data streams• May run queries against on-line databases• Sends alerts to human operator and/or other
automated systems with specific recommendations (e.g., sell, buy, limit order, short, call, put)
• Timeliness of outputs is crucial– A functionally correct output sent too late can be
worse than no output at all• Several application layers compete in real-time for
system resources (i.e., CPU, memory)
Real-Time Java Programming
Christopher D. Gill
Example: Stock Market Analysis Tool• Inputs arrive in real-
time from data streams– Real-time (seconds)
arrival of data events– One feed per market
• May run queries on-line tables and databases: differences in latency and latency jitter– Analyst reports– Market histories– Sector P/E tables
Example: Stock Market Analysis Tool// Data Store Query Code, Continuedpublic abstract class DataStore { public abstract void annotateAlert (Alert a);}public class NasdaqStore extends DataStore{ public float getPE (String symbol, boolean sector) {/* medium duration */} public float getEarnings (String symbol) {/*...*/} public void annotateAlert (Alert a) { addNasdaqAnnotation (a); /* ... */ } protected void addNasdaqAnnotation (Alert a) { float e = 0.0F; float r = 0.0F; // compute PE and Earnings averages for the sector a.addAnnotation (new NasdaqAnnotation (e, r)); }}
Real-Time Java Programming
annotations
sector analysis
table
P/E
Nasdaqmarkethistory
database
analysisquery
Christopher D. Gill
Example: Stock Market Analysis Tool// Data Store Query Code, Continuedpublic class ResearchStore extends DataStore{ public void annotateAlert (Alert a) { addResearchAnnotation (a);} protected void addResearchAnnotation (Alert a) { // long duration: guided // search for research // reports, adding URLS // for relevant analyst // research reports to // the annotation // (ordered by relevance // & confidence factors)
// add annotation to alert a.addAnnotation (new ResearchAnnotation ()); }} /* ... Other DataStore Classes ... */
Example: Stock Market Analysis Tool// Analysis Filter Codepublic class AlertList{// Alerts raised so far private java.util.Vector alerts; public void addAlert (Alert a) {alerts.add (a);} public Alert nextReport (boolean restart) { /* ... */ } public void reset () { alerts.clear ();}}
public abstract class AnalysisFilter {public abstract boolean handleDataEvent (DataFeedEvent d, AlertList a); // ...}
Real-Time Java Programming
data event
data feedAnalysis filter
alert list
Christopher D. Gill
Example: Stock Market Analysis Tool// Analysis Filter Code, Continuedpublic class CompositeFilter extends AnalysisFilter { // the composed filters private java.util.Vector filters; public void addFilter (AnalysisFilter af) { filters.add (af); } public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; for (int i = 0; !consumed && i < filters.size (); ++i) { consumed = ((AnalysisFilter) filters.get(i)).handleDataEvent (dfe, al); } return consumed; }}
Real-Time Java Programming
data event
Composite Filter
Christopher D. Gill
Example: Stock Market Analysis Tool// Analysis Filter Code, Continuedpublic class SectorPEFilter extends AnalysisFilter { private NasdaqStore nh; private ResearchStore rr; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; // See if event is of interest, // compare its PE to the avg for // its sector, look at existing // alerts, possibly generate // new ones annotated with // relevant research reports rr.annotateAlert (alert) return consumed; }}
Real-Time Java Programming
research reports
sector analysis
table
Sector P/E Filter
data event
Christopher D. Gill
Example: Stock Market Analysis Tool// Analysis Filter Code, Continuedpublic class Portfolio { public float projectRiskDelta (DataFeedEvent d) {/*...*/} public float projectGainDelta (DataFeedEvent d) {/*...*/}}public class PortfolioBalanceFilter extends AnalysisFilter { protected Portfolio p; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; // issue/remove alerts based on // data feed event and projected // risk/gain to portfolio goals return consumed; }}
Real-Time Java Programming
PortfolioBalance Filter
alert list
goals
risk profile
data event
Christopher D. Gill
Example: Stock Market Analysis Tool// Analysis Pipeline Codepublic class AnalysisPipeline{ private CompositeFilter cf; private DataFeed df; private AlertList al; public void addFilter (AnalysisFilter af) {cf.addFilter (af);} public void sendAlerts () {/* Send all alerts, reset list */} public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent (); cf.handleDataEvent (dfe, al); // possibly long latency sendAlerts (); /* latency depends on alert count */} }}
Real-Time Java Programming
data event
Filter Pipeline
alert list
send alerts
Operator
Christopher D. Gill
Example: Stock Market Analysis Tool// Analysis Tool Codepublic class AnalysisTool { public static void main (String [] args) { AnalysisPipeline ap = new AnalysisPipeline (); ap.addFilter (new PortfolioBalanceFilter ()); ap.addFilter (new SectorPEFilter ());
// Separate low latency activitypublic class AlertThreadAdapter implements Runnable{ private AnalysisPipeline pipeline; private long timeout; public AlertThreadAdapter (AnalysisPipeline ap, long t) { pipeline = ap; timeout = t;} public void run () { for (;;) // in reality, could use more sophisticated { // loop control e.g., wait, notifyAll, etc. try { Thread.sleep (timeout); pipeline.sendAlerts (); } catch (java.lang.InterruptedException e) {/* ... */} } }}
// Separate medium latency activitypublic class AnalysisPipeline{ private CompositeFilter cf; // filters in the pipeline private DataFeed df; // paced data event feed private AlertList al; // list of alerts public void addFilter (AnalysisFilter af) {cf.addFilter (af);} public void sendAlerts () {/* Send all alerts in the list, reset alert list */} public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent (); cf.handleDataEvent (dfe, al); // possibly long latency } }}
Real-Time Java Programming
Christopher D. Gill
Java: Threading Issues// Analysis Tool Code, Revisitedpublic class AnalysisTool { public static void main (String [] args) { AnalysisPipeline ap = new AnalysisPipeline (); ap.addFilter (new PortfolioBalanceFilter ()); ap.addFilter (new SectorPEFilter ());
Thread alertThread = new Thread (new AlertThreadAdapter (ap, 1000)); alertThread.setPriority (Thread.MAX_PRIORITY); alertThread.start (); ap.run (); // run pipeline in the current thread }}
Real-Time Java Programming
Christopher D. Gill
Java: Synchronization Issues• But, before we go
further addressing liveness issues, need to address concurrency safety
• Shift to top-down safety-first design mode, using fine-grain synchronization (Lea, “Concurrent Programming in JavaTM”)
• We’ll combine two styles: block and method synchronization
// Concurrency safety additions// using method synchronizationpublic abstract class Alert{ /* ... */ public synchronized void addAnnotation (Annotation a) {/* ...*/}
public synchronized Annotation nextAnnotation (boolean restart) {/*...*/}}
Real-Time Java Programming
Christopher D. Gill
Java: Synchronization Issues// Concurrency safety additions using block synchronization public class AnalysisPipeline{ /* ... */ protected void sendAlerts () { synchronized (al) {/* Send all the alerts in the list, reset alert list */} } public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent ();
// spawns separate threads for long latency activities cf.handleDataEvent (dfe, al); }}
Real-Time Java Programming
Christopher D. Gill
Java: Synchronization Issues// Concurrency safety additions using block synchronization
public class PortfolioBalanceFilter extends AnalysisFilter { protected Portfolio p; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; synchronized (al) { /* add alerts based on data feed event and the projected risk and gain changes to portfolio */ } return consumed; }}
Real-Time Java Programming
Christopher D. Gill
Java: Synchronization Issues// Concurrency safety additions using block synchronization public class SectorPEFilter extends AnalysisFilter { private NasdaqStore nh; private ResearchStore rr; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; /* compare PE to the average for its sector */ synchronized (al) { /* look at existing alerts*/ } /* possibly generate new ones, annotated in a separate thread with relevant research reports... */ synchronized (al) { /* add any new alerts to the list */ } return consumed; }}
The RTSJ and Real-Time Issues• Threads (revisited)• Release characteristics & failures• Scheduling• Synchronization (revisited)• Time and timers• Asynchronous event handling• Memory management• Asynchronous transfer of control• Exceptions• System-level options
Real-Time Java Programming
Christopher D. Gill
RT Issues: Threads• Multi-threading is useful to
decouple different activities– Active objects, request
queues, synch/asynch
• Must ensure resource usage by non-critical activities does not interfere with needs of critical activities
• However, work in different threads competes for CPU time and memory resources
Real-Time Java Programming
Christopher D. Gill
RTSJ: Threading Issues• Threads compete for
time on the CPU• Some activities are
higher priority than others
• Java thread priorities take us a step in the right direction, but…– garbage collector
thread priority and preemption issues
– Non-RT priority uniqueness is not ensured
// Solution: real-time threads
AlertThreadAdapter alertAdapter = new AlertThreadAdapter (ap, 1000);
javax.realtime.RealtimeThread alertThread = new javax.realtime.RealtimeThread (alertAdapter);
javax.realtime.RealtimeThread pipelineThread = new javax.realtime.RealtimeThread (ap);
alertThread.start ();pipelineThread.start ();
Real-Time Java Programming
Christopher D. Gill
RTSJ: Threading Issues// To run the pipeline in a Realtime thread, it could just
implement Runnable: for AnalysisPipeline this is not very invasive so we’ll skip writing a separate adapter
public class AnalysisPipeline{ /* ... */ protected void sendAlerts () { synchronized (al) {/* Send all the alerts in the list, reset alert list */} } public void run () { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent ();
// spawns separate threads for long latency activities cf.handleDataEvent (dfe, al); }}
implements Runnable
Real-Time Java Programming
Christopher D. Gill
RT Issues: Release Characteristics• To know whether threads
will interfere, need to characterize their temporal behavior
Time
execution cost
period
minimum inter-arrival spacing
deadline
• Can abstract out separate descriptors for canonical behavioral classes– I.e., periodic,
aperiodic, sporadic
• Need descriptors with key temporal attributes– E.g., execution cost,
deadline
Real-Time Java Programming
Christopher D. Gill
RTSJ: Release Characteristics Issues• While threading
allows priority partitioning, specific information and/or constraints on threads are needed
• Must ensure sufficient resources are available and correctly managed for desired behavior
javax.realtime.RelativeTime cost = new javax.realtime.RelativeTime (100, 0);
javax.realtime.RelativeTime period = new javax.realtime.RelativeTime (1000, 0);
public class AlertThreadAdapter implements javax.realtime.Schedulable
{ /* we can & should get/set release parameters, scheduling parameters, memory parameters, ... */
public void run () {addToFeasibility (); javax.realtime.RealtimeThread t = (javax.realtime.RealtimeThread) Thread.currentThread (); for (;;) { t.waitForNextPeriod (); // respect advertised cost, period times pipeline.sendAlerts (); } }}
Real-Time Java Programming
Christopher D. Gill
• Release characteristics advertise how threads are projected to behave
Time
actual execution cost
RT Issues: Release Failures
deadline
execution finished (late)
projected execution cost • However, differences between projected and actual behavior can lead to unexpected failures• Need to be able to detect (and if possible handle) release failures– Cost overruns– Deadline misses
Real-Time Java Programming
Christopher D. Gill
RTSJ: Release Failure Issues• Differences between
projected and expected behavior result in release failures– Execution
overruns– Deadline misses
• Can install a handler for each release characteristics instance to at least record, and possibly correct, failures
public class CostOverrunEventHandler extends javax.realtime.AsyncEventHandler { public void handleAsyncEvent() {/* ... */}}public class DeadlineMissEventHandler extends javax.realtime.AsyncEventHandler { public void handleAsyncEvent() {/* ... */}}
javax.realtime.PeriodicParameters pp = new javax.realtime.PeriodicParameters (null, // start immediately,
period, cost, null, // deadline = period end
new CostOverrunEventHandler (), new DeadlineMissEventHandler ()); alertThread.setReleaseParameters (pp);alertAdapter.setReleaseParameters (pp);alertThread.start ();
Real-Time Java Programming
Christopher D. Gill
RT Issues: Scheduling• Priorities
– Need sufficient unique priority levels
• Preemptive scheduling– Need well defined and
appropriate semantics• Fairness among threads
is not usually a Real-Time concern (FIFO vs. RR)– But may be useful
• Feasibility– Admission control,
certification/testing
sche
dule
r
blocked
runnable
executing
Real-Time Java Programming
Christopher D. Gill
RTSJ: Scheduling Issues• Release
characteristics give control over threads
• Scheduling addresses how to manage those threads
• Priority, preemption
• Feasibility
// Analysis Tool Code, Revisitedjavax.realtime.PriorityScheduler psched = (javax.realtime.PriorityScheduler) javax.realtime.Scheduler.getDefaultScheduler ();javax.realtime.PriorityParameters high = new javax.realtime.PriorityParameters
(psched.getMaxPriority ());javax.realtime.PriorityParameters med = new javax.realtime.PriorityParameters
RT Issues: Time and Timers• Time resolution needed
– Hours down to nsec
start expire
• Absolute time– Common temporal
reference, e.g., UTC
• Occurrences over time• Absolute clock• Timer mechanisms
– One-shot, periodic
• Relative Time– Since start of thread– Since last period
Real-Time Java Programming
Christopher D. Gill
RTSJ: Time and Timer Issues
• Threads offer a clean programming model
• However, many real-time systems benefit from asynchronous behavior
• Also, pacing is an effective/alternative way to reduce resource contention and improve resource utilization
// A needed solution: watchdog timerpublic class StoreTimeoutHandler extends javax.realtime.AsyncEventHandler{public void handleAsyncEvent() {/* ... */}}public class StoreThreadAdapter implements javax.realtime.Schedulable{ public void run () { // ... set up thread priorities ... long m = 60000; // one minute new javax.realtime.OneShotTimer (new javax.realtime.RelativeTime (m,0), new StoreTimeoutHandler ()); store.annotateAlert (alert); } // ... }
RTSJ: Memory Management Issues• Realtime threads get
higher priority than the garbage collector
• However, there is still a possibility of priority inversion– If GC is collecting
the heap, it must reach a “safe” state before RT threads can use the heap
• NoHeapRealtime threads avoid this
// Solution: separate memory areas and// no-heap real-time threads
javax.realtime.MemoryArea ma = new javax.realtime.LTMemory (initSize, maxSize);
javax.realtime.NoHeapRealtimeThread alertThread = new javax.realtime.NoHeapRealtimeThread (sp, // sched params rp, // release params mp, // memory params ma, // memory area pg, // processing group alertAdapter);
Real-Time Java Programming
Christopher D. Gill
RTSJ: Memory Management Issues// Immortal Memory is a Singleton
javax.realtime.MemoryArea im = javax.realtime.ImmortalMemory.instance ();
im.enter (this); // this must be Runnable // allocates memory on // the ImmortalMemory area // until another memory // area is entered, or // the Runnable run () // call exits and enter () // returns
• Scoped memory is key for no-heap real-time threads
• Other kinds of MemoryArea– Immortal
Memory: can improve GC performance
• Physical Memory– Immortal,
scoped, raw– Factory
Real-Time Java Programming
Christopher D. Gill
RT Issues: Asynch Transfer of Control• Want to provide real-
time behavior for long-running synchronous activities (e.g., searches)
• For fault-tolerance, some activities may need to be halted immediately
• However, standard threading and interrupt semantics can produce undefined/deadlock behavior in many common use-cases
• ATC refines semantics
Publisher
Exhaustive Lookup
Shipper
“findanythingrelevant”
“stop and giveme what you havefound so far”
searching
Real-Time Java Programming
Christopher D. Gill
RTSJ: ATC Issues• Even with the one-shot
timer, the long running-thread must be reigned in somehow
• Deprecated Thread stop, suspend calls are unsafe
• ATC defers exception as pending in synchronized methods – avoids problem w/deprecated Thread stop method