Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 *JavaTM is a registered trademark of Sun Microsystems Real-Time Java Programming 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 Christopher D. Gill Real-Time Java Programming 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) Christopher D. Gill Real-Time Java Programming Example: Stock Market Analysis Tool • Inputs arrive in real-time from data streams – Real-time (seconds) arrival of data events NasdaqFeed – One feed per market • May run queries on-line DataStore tables and databases: differences in latency and latency jitter – Analyst reports NasdaqStore – Market histories – Sector P/E tables ResearchStore DataFeed NYSEFeed NYSEStore Christopher D. Gill Real-Time Java Programming Example: Stock Market Analysis Tool Alert MarketOrder Buy Sell Christopher D. Gill • Sends recommendations as alerts to: * Annotation – Human operators – Automated systems • Documented quality of information is key Option – Decision path, triggers – Additional info, links Call Put • Timeliness constraints must also be met – Incremental addition, refinement is useful Real-Time Java Programming Example: Stock Market Analysis Tool 1+ AnalysisFilter SectorPE 1+ PortfolioBalance AnalysisPipeline Composite • Input events pass through an analysis pipeline – Each analysis filter handles the data and news events in which it is interested, may search databases – May attach additional information to event and pass it on or consume it, and/or produce alerts – Composites combine other analysis filters Christopher D. Gill Real-Time Java Programming Example: Roadmap AnalysisTool AnalysisPipeline AnalysisFilter AlertList CompositeFilter DataFeed SectorPEFilter DataFeedEvent PortfolioBalanceFilter Annotation Portfolio AnnotationList Alert NasdaqDataFeed ResearchAnnotation MarketOrderAlert OptionAlert DataStore NasdaqAnnotation CallAlert NasdaqStore ResearchStore BuyAlert Christopher D. Gill SellAlert PutAlert Real-Time Java Programming Example: Stock Market Analysis Tool // Input Event Streams Code market order public class DataFeedEvent { private float bid; private float ask; 90 seconds private float change; data private long volume; event // ... public DataFeedEvent data feed (float b, float a, float c, long v) {bid = b; ask = a; change = c; volume = v;} public float getBid () {return bid;} public float getAsk () {return ask;} public float getChange () {return change;} public long getVolume () {return volume;} // ... } Christopher D. Gill Market Real-Time Java Programming Example: Stock Market Analysis Tool // Input Event Streams Code, Continued public abstract class DataFeed { public abstract DataFeedEvent pullDataFeedEvent (); } public class NasdaqDataFeed extends DataFeed { // low-ish latency public DataFeedEvent pullDataFeedEvent () { return pullNasdaqDataFeedEvent (); } protected DataFeedEvent pullNasdaqDataFeedEvent () { float bid = 0.0F; float ask = 0.0F; float chg = 0.0F; long vol = 0; // read data from socket, etc... return new DataFeedEvent (bid, ask, chg, vol); } } /* ... Other DataFeed Classes ... */ Christopher D. Gill • Separate data feed for each market • Low latency to pull an event from a market data feed Real-Time Java Programming Example: Roadmap AnalysisTool AnalysisPipeline AnalysisFilter AlertList CompositeFilter DataFeed SectorPEFilter DataFeedEvent NasdaqDataFeed PortfolioBalanceFilter Annotation ResearchAnnotation Portfolio AnnotationList MarketOrderAlert Alert OptionAlert DataStore NasdaqAnnotation CallAlert NasdaqStore ResearchStore BuyAlert Christopher D. Gill SellAlert PutAlert Real-Time Java Programming Example: Stock Market Analysis Tool // Alerts Code public abstract class Annotation { /* ... */ } public class AnnotationList { private java.util.Vector alist; // list of annotations public void addSorted (Annotation a) { /* ... */ } } public abstract class Alert { private AnnotationList anotes; private DataFeedEvent trigger; Alert (DataFeedEvent dfe) {anotes = new AnnotationList (); trigger = dfe;} public DataFeedEvent getTrigger () {return trigger;} public void addAnnotation (Annotation a) { anotes.addSorted (a); } public Annotation nextAnnotation (boolean restart) { /* move to next annotation in list, return it ... */ } } Christopher D. Gill Alert trigger annotations Real-Time Java Programming Example: Stock Market Analysis Tool // Alerts Code, Continued public abstract class MarketOrderAlert extends Alert { private float orderPrice; private String symbol; public MarketOrderAlert (DataFeedEvent dfe, float op, String s) {super (dfe); orderPrice = op; symbol = s;} protected String getSymbol () {return symbol;} protected float getOrderPrice () {return orderPrice;} } /* ... Similarly, for OptionAlert and its derived classes public class BuyAlert extends MarketOrderAlert { public BuyAlert (DataFeedEvent dfe, float op, String s) {super (dfe, op, s);} float getBuyPrice () { return super.getOrderPrice (); } } /* ... Similarly for SellAlert, Other Alert Classes ... */ Christopher D. Gill ... */ Real-Time Java Programming Example: Stock Market Analysis Tool // Data Store Query Code public class NasdaqAnnotation extends Annotation annotations { private float sectorAvgEarnings; private float sectorPERatio; public NasdaqAnnotation (float e, float r) {sectorAvgEarnings = e; sectorPERatio = r;} P/E public float getSectorAvgEarnings () URL {return sectorAvgEarnings;} public float getSectorPERatio () sector {return sectorPERatio;} analysis }/* ... Other Annotation Classes */ table public class ResearchAnnotation research extends Annotation reports { // URLs for research reports private java.util.Vector research_reports; public void addReport (java.net.URL u) {reports.add (u);} public java.net.URL nextReport (boolean restart) { /* ... */ } }/* ... Other Annotation Classes */ Christopher D. Gill Real-Time Java Programming Example: Roadmap AnalysisTool AnalysisPipeline AnalysisFilter AlertList CompositeFilter DataFeed SectorPEFilter DataFeedEvent NasdaqDataFeed PortfolioBalanceFilter Annotation ResearchAnnotation Portfolio AnnotationList MarketOrderAlert Alert OptionAlert DataStore NasdaqAnnotation CallAlert NasdaqStore ResearchStore BuyAlert Christopher D. Gill SellAlert PutAlert Real-Time Java Programming Example: Stock Market Analysis Tool // Data Store Query Code, Continued public abstract class DataStore annotations { public abstract void annotateAlert (Alert a);} public class NasdaqStore extends DataStore P/E { public float getPE (String symbol, boolean sector) {/* medium duration */} sector public float getEarnings analysis analysis (String symbol) {/*...*/} query table public void annotateAlert (Alert a) { addNasdaqAnnotation (a); /* ... */ } protected void addNasdaqAnnotation (Alert a) Nasdaq { float e = 0.0F; float r = 0.0F; market // compute PE and Earnings averages for the sector history a.addAnnotation (new NasdaqAnnotation (e, r)); database } } Christopher D. Gill Real-Time Java Programming Example: Stock Market Analysis Tool // Data Store Query Code, Continued public 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 report // research reports to index // the annotation // (ordered by relevance // & confidence factors) // add annotation to alert a.addAnnotation (new ResearchAnnotation ()); } } /* ... Other DataStore Classes ... */ Christopher D. Gill annotations URL search agent hyperlinked research reports Real-Time Java Programming Example: Roadmap AnalysisTool AnalysisPipeline AnalysisFilter AlertList CompositeFilter DataFeed SectorPEFilter DataFeedEvent NasdaqDataFeed PortfolioBalanceFilter Annotation ResearchAnnotation Portfolio AnnotationList MarketOrderAlert Alert OptionAlert DataStore NasdaqAnnotation CallAlert NasdaqStore ResearchStore BuyAlert Christopher D. Gill SellAlert PutAlert Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Filter Code public 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 ();} Analysis filter } public abstract class AnalysisFilter {public abstract boolean handleDataEvent (DataFeedEvent d, AlertList a); // ... } Christopher D. Gill alert list data event data feed Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Filter Code, Continued public class CompositeFilter extends AnalysisFilter { // the composed filters data private java.util.Vector filters; event public void addFilter (AnalysisFilter af) { filters.add (af); } public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; Composite for (int i = 0; Filter !consumed && i < filters.size (); ++i) { consumed = ((AnalysisFilter) filters.get(i)).handleDataEvent (dfe, al); } return consumed; } } Christopher D. Gill Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Filter Code, Continued public class SectorPEFilter extends AnalysisFilter { private NasdaqStore nh; private ResearchStore rr; data public boolean handleDataEvent event (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 sector // alerts, possibly generate analysis // new ones annotated with table // relevant research reports rr.annotateAlert (alert) return consumed; } } Christopher D. Gill Sector P/E Filter research reports Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Filter Code, Continued public class Portfolio { public float projectRiskDelta (DataFeedEvent d) {/*...*/} public float projectGainDelta (DataFeedEvent d) {/*...*/} } public class PortfolioBalanceFilter data event extends AnalysisFilter Portfolio { protected Portfolio p; Balance Filter 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 goals return consumed; } } Christopher D. Gill alert list risk profile Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Pipeline Code alert send public class AnalysisPipeline list alerts { private CompositeFilter cf; private DataFeed df; private AlertList al; data public void addFilter event Operator (AnalysisFilter af) {cf.addFilter (af);} public void sendAlerts () {/* Send all alerts, reset list */} Filter public void run () Pipeline { for (;;) { DataFeedEvent dfe = df.pullDataFeedEvent (); cf.handleDataEvent (dfe, al); // possibly long latency sendAlerts (); /* latency depends on alert count */} } } Christopher D. Gill Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Tool Code public class AnalysisTool { public static void main (String [] args) { AnalysisPipeline ap = new AnalysisPipeline (); ap.addFilter (new PortfolioBalanceFilter ()); ap.addFilter (new SectorPEFilter ()); ap.run (); // run the pipeline } } Christopher D. Gill alert list send alerts market order data event data feed Market Real-Time Java Programming Review: Roadmap AnalysisTool AnalysisPipeline AnalysisFilter AlertList CompositeFilter DataFeed SectorPEFilter DataFeedEvent PortfolioBalanceFilter Annotation Portfolio AnnotationList Alert NasdaqDataFeed ResearchAnnotation MarketOrderAlert OptionAlert DataStore NasdaqAnnotation CallAlert NasdaqStore ResearchStore BuyAlert Christopher D. Gill SellAlert PutAlert Real-Time Java Programming Example: Time Scales AnalysisTool AnalysisPipeline AnalysisFilter AlertList CompositeFilter DataFeed SectorPEFilter DataFeedEvent NasdaqDataFeed PortfolioBalanceFilter Annotation ResearchAnnotation Portfolio AnnotationList MarketOrderAlert Alert OptionAlert DataStore NasdaqAnnotation CallAlert NasdaqStore Latency: ResearchStore Low Christopher D. Gill Medium High BuyAlert SellAlert PutAlert Real-Time Java Programming Java Real-Time Issues • Existing JavaTM facilities take us several important steps in the direction of real-time application behavior • Threads – Liveness (what and how much happens) – Threads are used to decouple activity time scales • Synchronization – Safety (nothing “unsafe” happens) – Careful application of monitors can preserve liveness • We’ll start in a bottom-up liveness-first design mode, using thread adapters (Lea, “Concurrent Programming in JavaTM”) Christopher D. Gill Real-Time Java Programming Java: Threading Issues • Separate threads of execution are useful to improve liveness by doing // Separate high latency activity public class StoreThreadAdapter the following concurrently: implements Runnable – Getting and handling { private DataStore store; market data events private Alert alert; public StoreThreadAdapter • Medium latency (DataStore ds, Alert a) – Searching stores to add { store = ds; alert = a;} annotations public void run () { • High latency store.annotateAlert (alert); – Issuing alerts } • Low latency } // Analysis Tool Code, Revisited Christopher D. Gill Real-Time Java Programming Java: Threading Issues // Analysis Filter Code, Revisited public class SectorPEFilter extends AnalysisFilter { private NasdaqStore nh; private ResearchStore rr; public boolean handleDataEvent (DataFeedEvent dfe, AlertList al) { boolean consumed = false; // possibly generate new alerts ... // ... annotated with relevant research reports... Thread annotationThread = new Thread (new StoreThreadAdapter (rr, alert)); annotationThread.setPriority (Thread.MIN_PRIORITY); annotationThread.start (); return consumed; } } Christopher D. Gill Real-Time Java Programming Java: Threading Issues // Analysis Tool Code, Revisited // Separate low latency activity public 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) {/* ... */} } } } Christopher D. Gill Real-Time Java Programming Java: Threading Issues // Analysis Pipeline Code, Revisited // Separate medium latency activity public 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 } } } Christopher D. Gill Real-Time Java Programming Java: Threading Issues // Analysis Tool Code, Revisited public 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 } } Christopher D. Gill Real-Time Java Programming Java: Synchronization Issues // Concurrency safety additions // using method synchronization public abstract class Alert { /* ... */ public synchronized void addAnnotation (Annotation a) {/* ...*/} public synchronized Annotation nextAnnotation (boolean restart) {/*...*/} } Christopher D. Gill • But, before we go further addressing liveness issues, need to address concurrency safety • Shift to top-down safetyfirst design mode, using fine-grain synchronization (Lea, “Concurrent Programming in JavaTM”) • We’ll combine two styles: block and method synchronization Real-Time Java Programming 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); } } Christopher D. Gill Real-Time Java Programming 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; } } Christopher D. Gill Real-Time Java Programming 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; } } Christopher D. Gill Real-Time Java Programming Threads and Synch Points AnalysisTool medium AnalysisPipeline AnalysisFilter latency low latency CompositeFilter DataFeed SectorPEFilter DataFeedEvent PortfolioBalanceFilter Annotation AlertList Portfolio AnnotationList Alert NasdaqDataFeed ResearchAnnotation DataStore high latency MarketOrderAlert OptionAlert NasdaqAnnotation CallAlert NasdaqStore ResearchStore BuyAlert Synchronization points: Christopher D. Gill SellAlert PutAlert Real-Time Java Programming 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 Christopher D. Gill Real-Time Java Programming RT Issues: Threads • Multi-threading is useful to decouple different activities – Active objects, request queues, synch/asynch • However, work in different threads competes for CPU time and memory resources • Must ensure resource usage by non-critical activities does not interfere with needs of critical activities Christopher D. Gill Real-Time Java Programming RTSJ: Threading Issues • Threads compete for time on the CPU AlertThreadAdapter alertAdapter = • Some activities are higher new AlertThreadAdapter (ap, 1000); priority than others javax.realtime.RealtimeThread • Java thread priorities take alertThread = new us a step in the right javax.realtime.RealtimeThread direction, but… (alertAdapter); – garbage collector javax.realtime.RealtimeThread thread priority and pipelineThread = preemption issues new javax.realtime.RealtimeThread (ap); – Non-RT priority uniqueness is not alertThread.start (); ensured pipelineThread.start (); // Solution: real-time threads Christopher D. Gill Real-Time Java Programming 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 implements Runnable { /* ... */ 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); } } Christopher D. Gill Real-Time Java Programming RT Issues: Release Characteristics execution cost period deadline minimum inter-arrival spacing Time Christopher D. Gill • To know whether threads will interfere, need to characterize their temporal behavior • Need descriptors with key temporal attributes – E.g., execution cost, deadline • Can abstract out separate descriptors for canonical behavioral classes – I.e., periodic, aperiodic, sporadic Real-Time Java Programming RTSJ: Release Characteristics Issues • javax.realtime.RelativeTime cost = new javax.realtime.RelativeTime (100, 0); While threading allows priority partitioning, specific javax.realtime.RelativeTime period = information and/or new javax.realtime.RelativeTime (1000, 0); constraints on threads are needed javax.realtime.PeriodicParameters pp = new • Must ensure javax.realtime.PeriodicParameters ( null, // start immediately, sufficient resources period, cost, are available and null, // deadline = period end correctly managed null, null); for desired behavior alertThread.setReleaseParameters (pp); alertThread.start (); Christopher D. Gill Real-Time Java Programming RTSJ: Release Characteristics Issues // Analysis Tool Code, Revisited 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 (); } } } Christopher D. Gill Real-Time Java Programming RT Issues: Release Failures actual execution cost projected execution cost execution finished (late) deadline Time Christopher D. Gill • Release characteristics advertise how threads are projected to behave • 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 RTSJ: Release Failure Issues 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 (); Christopher D. Gill 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 Real-Time Java Programming RT Issues: Scheduling executing blocked Christopher D. Gill scheduler runnable • 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 Real-Time Java Programming RTSJ: Scheduling Issues // Analysis Tool Code, Revisited • javax.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 (psched.getNormPriority ()); try { alertThread.setSchedulingParameters (high); • pipelineThread. setSchedulingParameters (med); } catch (java.lang.IllegalArgumentException e) {/* ... */} • alertThread.start (); pipelineThread.start (); Christopher D. Gill Release characteristics give control over threads Scheduling addresses how to manage those threads Priority, preemption Feasibility Real-Time Java Programming RTSJ: Scheduling Issues // Analysis Tool Code, Revisited public class StoreThreadAdapter implements javax.realtime.Schedulable {/* ... */ public void run () { javax.realtime.PriorityScheduler psched = (javax.realtime.PriorityScheduler) javax.realtime.Scheduler.getDefaultScheduler (); try { javax.realtime.PriorityParameters pp = new javax.realtime.PriorityParameters (psched.getMinPriority ()); setSchedulingParameters (pp); javax.realtime.RealtimeThread t = (javax.realtime.RealtimeThread) Thread.currentThread (); t.setSchedulingParameters (pp); } catch (java.lang.IllegalArgumentException e) {/* ... */} store.annotateAlert (alert); } } Christopher D. Gill Real-Time Java Programming RT Issues: Synchronization running outside block synchronized block blocked at guard waiting (blocked) on a condition variable running inside block priority key: high Christopher D. Gill middle low • Risk of unbounded priority inversions – Canonical high, low, middle scenario • Priorities can uncover or exacerbate “bad” executions of existing race conditions – Horstmann & Cornell, ”Core Java 2” • Need well defined thread and locking semantics Real-Time Java Programming RTSJ: Synchronization Issues • Real-time threads at // Solution: Monitor Control • javax.realtime.MonitorControl.setMonitorControl (new javax.realtime.PriorityInheritance ()); • // Solution: wait-free queues public class StoreThreadAdapter implements javax.realtime.Schedulable { • /* ... */ private javax.realtime.WaitFreeDequeue dequeue; /* ... */ } • Christopher D. Gill different priorities share resources However, this presents new real-time issues – Priority inversions Need additional mechanisms to ensure priority-safe sharing – Monitor Control Methods wait and notifyAll still work (avoid notify unless absolutely sure OK) – But, add overhead Non-blocking R/W queues: thread glue Real-Time Java Programming RT Issues: Time and Timers • Time resolution needed – Hours down to nsec • Relative Time – Since start of thread – Since last period • Absolute time – Common temporal reference, e.g., UTC start expire Christopher D. Gill • Occurrences over time • Absolute clock • Timer mechanisms – One-shot, periodic Real-Time Java Programming RTSJ: Time and Timer Issues // A needed solution: watchdog timer public 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); } // ... } Christopher D. Gill Threads offer a clean programming model However, many realtime systems benefit from asynchronous behavior Also, pacing is an effective/alternative way to reduce resource contention and improve resource utilization Real-Time Java Programming RT Issues: Asynch Event Handling handler method handler Christopher D. Gill • Threads allow synchronous programming styles • Sometimes, asynchronous styles are more appropriate – Real-world timing issues event – Decoupling processing • Events-and-handlers model provides mechanisms for: – Synchronous –> threads – Asynchronous –> timers – Mixed –> half-synch / halfasynch pattern Real-Time Java Programming RTSJ: Asynch Event Handling Issues // Another way to implement periodicity public class TransmitTimeoutHandler extends javax.realtime.AsyncEventHandler {public void handleAsyncEvent () {/*...*/}} new javax.realtime.PeriodicTimer (null, new javax.realtime.RelativeTime (1000, 0), new TransmitTimeoutHandler ()); Christopher D. Gill • We saw an earlier example of a oneshot timer used to determine when a long-running thread had been gone too long • Could also use a periodic timer to reimplement the high priority alert transmission code Real-Time Java Programming RT Issues: Memory Management memory manager Christopher D. Gill • Bounded allocation times • Managed vs. raw access – Trade-off in control vs. responsibility • Memory lifetimes – Program, local scope • Resource use descriptors • Application/manager interactions – Priority inversions – Memory contention • Safety and liveness Real-Time Java Programming RTSJ: Memory Management Issues // 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); Christopher D. Gill • 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 Real-Time Java Programming RTSJ: Memory Management Issues • Scoped memory is // Immortal Memory is a Singleton key for no-heap realtime threads javax.realtime.MemoryArea im = javax.realtime.ImmortalMemory.instance (); • Other kinds of MemoryArea im.enter (this); // this must be Runnable – Immortal Memory: // allocates memory on can improve GC // the ImmortalMemory area performance // until another memory • Physical Memory // area is entered, or // the Runnable run () – Immortal, scoped, // call exits and enter () raw // returns – Factory Christopher D. Gill Real-Time Java Programming RT Issues: Asynch Transfer of Control Publisher “find anything relevant” Shipper Exhaustive Lookup searching Christopher D. Gill “stop and give me what you have found so far” • 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 Real-Time Java Programming RTSJ: ATC Issues // Data Store Query Code, Revisited public abstract class DataStore { /* ... */ public abstract void annotateAlert (Alert a) • Even with the one-shot timer, the long runningthread must be reigned in somehow • Deprecated Thread stop, suspend calls are unsafe throws javax.realtime.AsynchronouslyInterruptedException; } // In timer handling for // StoreThreadAdapter run () t.interrupt (); Christopher D. Gill • ATC defers exception as pending in synchronized methods – avoids problem w/deprecated Thread stop method Real-Time Java Programming RT Issues: Exceptions safe scope caught propagates (re)thrown raised “tunnels” unsafe scope Christopher D. Gill • Additional special-purpose exceptions w/ standard semantics for – Memory management – Synchronization – System resource management • Special semantics for ATC – When to throw (or not) – Deferred propagation semantics (“exception tunneling”) - safety – Nesting/replacement Real-Time Java Programming RTSJ: Exceptions Issues • Semantics for AIE are different than others – deferred in pending state until inside a safe scope, where it will be thrown • Other new exceptions deal primarily with incompatibilities of memory areas – Trying to assign a reference to scoped memory to a variable in immortal or heap memory – Setting up a WaitFreeQueue, exception propagation, etc. in an incompatible memory area – Raw memory allocation errors (offset, size) – Raw memory access errors Christopher D. Gill Real-Time Java Programming RT Issues: System-level Options SIGKILL SIGINT SIGABRT getManager setManager Christopher D. Gill security manager • Although strict layering is often desirable, platformspecific issues tend to peek through – E.g., signals, schedulers • Collecting the system-wide constants, methods, etc. under one or more classes reduces pollution and improves the programming model • May add points of configurability (I.e., various system-wide managers) Real-Time Java Programming RTSJ: System-level Options Issues • javax.realtime.RealtimeSystem is analogous to java.lang.System – Gives access to real-time system properties • E.g., concurrent locks, endian properties – Allows a RealtimeSecurity manager to be set as the system security manager – Gives access to the current garbage collector • PosixSignalHandler – Required on platforms that provide POSIX signals – Thus, can only be used portably among those implementations Christopher D. Gill Real-Time Java Programming Review: Time Scales AnalysisTool AnalysisPipeline AnalysisFilter AlertList CompositeFilter DataFeed SectorPEFilter DataFeedEvent NasdaqDataFeed PortfolioBalanceFilter Annotation ResearchAnnotation Portfolio AnnotationList MarketOrderAlert Alert OptionAlert DataStore NasdaqAnnotation CallAlert NasdaqStore Latency: ResearchStore Low Christopher D. Gill Medium High BuyAlert SellAlert PutAlert Real-Time Java Programming Review: Java, RTSJ, Real-Time Issues • • • • • • • • • • Threads (Java, revisited in RTSJ) Release characteristics & failures Scheduling Synchronization (Java, revisited in RTSJ) Time and timers Asynchronous event handling Memory management Asynchronous transfer of control Exceptions System-level options Christopher D. Gill Real-Time Java Programming Review: Java and RTSJ AnalysisTool medium duration timer AnalysisPipeline AnalysisFilter feasibile CompositeFilter DataFeed SectorPEFilter DataFeedEvent over-run handler low latency high priority latency AlertList real-time Portfolio periodic no heap scoped AnnotationList Alert memory priority inheritance PortfolioBalanceFilter Annotation NasdaqDataFeed ResearchAnnotation DataStore high latency aynch transfer of control NasdaqStore MarketOrderAlert NasdaqAnnotation CallAlert ResearchStore BuyAlert Synchronization points: Christopher D. Gill OptionAlert SellAlert PutAlert Real-Time Java Programming Concluding Remarks • The RTSJ extends and/or refines existing Java semantics to address issues of real-time concern – Priority control, memory management, release parameters, feasibility, … • However, the RTSJ largely stays within the existing programming model – Some new idioms to master, but much is preserved – ATC in particular illustrates the trade-offs • Stay tuned, more evolution is on the horizon – Reference implementations and benchmarking – New specification efforts, e.g., the DRTSJ (JSR 50) Christopher D. Gill