RealTime Java Programming Christopher D Gill cdgillcs wustl



































































- Slides: 67
Real-Time Java* Programming Christopher D. Gill cdgill@cs. wustl. edu Center for Distributed Object Computing Department of Computer Science Washington University, St. Louis http: //www. cs. wustl. edu/~cdgill/RTSJ/COOTS 01_M 4. ppt COOTS 2001 Tutorial M 4 Monday, January 29, 2001 *Java. TM 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 Java. TM (Java) programming environment – How features of the Real-Time Specification for Java. TM (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 Nasdaq. Feed – One feed per market • May run queries on-line Data. Store tables and databases: differences in latency and latency jitter – Analyst reports Nasdaq. Store – Market histories – Sector P/E tables Research. Store Data. Feed NYSEStore Christopher D. Gill
Real-Time Java Programming Example: Stock Market Analysis Tool Alert Market. Order 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+ Analysis. Filter Sector. PE 1+ Portfolio. Balance Analysis. Pipeline 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 Analysis. Tool Example: Roadmap Analysis. Pipeline Analysis. Filter Alert. List Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event Nasdaq. Data. Feed Portfolio. Balance. Filter Annotation Research. Annotation Portfolio Annotation. List Market. Order. Alert Option. Alert Data. Store Nasdaq. Annotation Call. Alert Nasdaq. Store Research. Store Buy. Alert Christopher D. Gill Sell. Alert Put. Alert
Real-Time Java Programming Example: Stock Market Analysis Tool // Input Event Streams Code market order public class Data. Feed. Event { private float bid; private float ask; 90 seconds private float change; data private long volume; event //. . . public Data. Feed. Event data feed (float b, float a, float c, long v) {bid = b; ask = a; change = c; volume = v; } public float get. Bid () {return bid; } public float get. Ask () {return ask; } public float get. Change () {return change; } public long get. Volume () {return volume; } //. . . } Christopher D. Gill Market
Real-Time Java Programming Example: Stock Market Analysis Tool // Input Event Streams Code, Continued public abstract class Data. Feed { public abstract Data. Feed. Event pull. Data. Feed. Event (); } public class Nasdaq. Data. Feed extends Data. Feed { // low-ish latency public Data. Feed. Event pull. Data. Feed. Event () { return pull. Nasdaq. Data. Feed. Event (); } protected Data. Feed. Event pull. Nasdaq. Data. Feed. Event () { float bid = 0. 0 F; float ask = 0. 0 F; float chg = 0. 0 F; long vol = 0; // read data from socket, etc. . . return new Data. Feed. Event (bid, ask, chg, vol); } } /*. . . Other Data. Feed 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 Analysis. Tool Example: Roadmap Analysis. Pipeline Analysis. Filter Alert. List Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event Nasdaq. Data. Feed Portfolio. Balance. Filter Annotation Research. Annotation Data. Store Portfolio Annotation. List Market. Order. Alert Option. Alert Nasdaq. Annotation Call. Alert Nasdaq. Store Research. Store Buy. Alert Christopher D. Gill Sell. Alert Put. Alert
Real-Time Java Programming Example: Stock Market Analysis Tool // Alerts Code public abstract class Annotation { /*. . . */ } public class Annotation. List { private java. util. Vector alist; // list of annotations public void add. Sorted (Annotation a) { /*. . . */ } } public abstract class Alert { private Annotation. List anotes; private Data. Feed. Event trigger; Alert (Data. Feed. Event dfe) {anotes = new Annotation. List (); trigger = dfe; } public Data. Feed. Event get. Trigger () {return trigger; } public void add. Annotation (Annotation a) { anotes. add. Sorted (a); } public Annotation next. Annotation (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 Market. Order. Alert extends Alert { private float order. Price; private String symbol; public Market. Order. Alert (Data. Feed. Event dfe, float op, String s) {super (dfe); order. Price = op; symbol = s; } protected String get. Symbol () {return symbol; } protected float get. Order. Price () {return order. Price; } } /*. . . Similarly, for Option. Alert and its derived classes public class Buy. Alert extends Market. Order. Alert { public Buy. Alert (Data. Feed. Event dfe, float op, String s) {super (dfe, op, s); } float get. Buy. Price () { return super. get. Order. Price (); } } /*. . . Similarly for Sell. Alert, Other Alert Classes. . . */ Christopher D. Gill . . . */
Real-Time Java Programming Example: Stock Market Analysis Tool // Data Store Query Code public class Nasdaq. Annotation extends Annotation annotations { private float sector. Avg. Earnings; private float sector. PERatio; public Nasdaq. Annotation (float e, float r) {sector. Avg. Earnings = e; sector. PERatio = r; } P/E public float get. Sector. Avg. Earnings () URL {return sector. Avg. Earnings; } public float get. Sector. PERatio () sector {return sector. PERatio; } analysis }/*. . . Other Annotation Classes */ table public class Research. Annotation research extends Annotation reports { // URLs for research reports private java. util. Vector research_reports; public void add. Report (java. net. URL u) {reports. add (u); } public java. net. URL next. Report (boolean restart) { /*. . . */ } }/*. . . Other Annotation Classes */ Christopher D. Gill
Real-Time Java Programming Analysis. Tool Example: Roadmap Analysis. Pipeline Analysis. Filter Alert. List Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event Nasdaq. Data. Feed Portfolio. Balance. Filter Annotation Research. Annotation Data. Store Portfolio Annotation. List Market. Order. Alert Option. Alert Nasdaq. Annotation Call. Alert Nasdaq. Store Research. Store Buy. Alert Christopher D. Gill Sell. Alert Put. Alert
Real-Time Java Programming Example: Stock Market Analysis Tool // Data Store Query Code, Continued public abstract class Data. Store annotations { public abstract void annotate. Alert (Alert a); } public class Nasdaq. Store extends Data. Store P/E { public float get. PE (String symbol, boolean sector) {/* medium duration */} sector public float get. Earnings analysis (String symbol) {/*. . . */} query table public void annotate. Alert (Alert a) { add. Nasdaq. Annotation (a); /*. . . */ } protected void add. Nasdaq. Annotation (Alert a) Nasdaq { float e = 0. 0 F; float r = 0. 0 F; market // compute PE and Earnings averages for the sector history a. add. Annotation (new Nasdaq. Annotation (e, r)); database } } Christopher D. Gill
Real-Time Java Programming Example: Stock Market Analysis Tool // Data Store Query Code, Continued public class Research. Store extends Data. Store { public void annotate. Alert (Alert a) { add. Research. Annotation (a); } protected void add. Research. Annotation (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. add. Annotation (new Research. Annotation ()); } } /*. . . Other Data. Store Classes. . . */ Christopher D. Gill annotations URL search agent hyperlinked research reports
Real-Time Java Programming Analysis. Tool Example: Roadmap Analysis. Pipeline Analysis. Filter Alert. List Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event Nasdaq. Data. Feed Portfolio. Balance. Filter Annotation Research. Annotation Data. Store Portfolio Annotation. List Market. Order. Alert Option. Alert Nasdaq. Annotation Call. Alert Nasdaq. Store Research. Store Buy. Alert Christopher D. Gill Sell. Alert Put. Alert
Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Filter Code public class Alert. List {// Alerts raised so far private java. util. Vector alerts; public void add. Alert (Alert a) {alerts. add (a); } public Alert next. Report (boolean restart) { /*. . . */ } public void reset () { alerts. clear (); } Analysis filter } public abstract class Analysis. Filter {public abstract boolean handle. Data. Event (Data. Feed. Event d, Alert. List 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 Composite. Filter extends Analysis. Filter { // the composed filters data private java. util. Vector filters; event public void add. Filter (Analysis. Filter af) { filters. add (af); } public boolean handle. Data. Event (Data. Feed. Event dfe, Alert. List al) { boolean consumed = false; Composite for (int i = 0; Filter !consumed && i < filters. size (); ++i) { consumed = ((Analysis. Filter) filters. get(i)). handle. Data. Event (dfe, al); } return consumed; } } Christopher D. Gill
Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Filter Code, Continued public class Sector. PEFilter extends Analysis. Filter { private Nasdaq. Store nh; private Research. Store rr; data public boolean handle. Data. Event event (Data. Feed. Event dfe, Alert. List 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. annotate. Alert (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 project. Risk. Delta (Data. Feed. Event d) {/*. . . */} public float project. Gain. Delta (Data. Feed. Event d) {/*. . . */} } public class Portfolio. Balance. Filter data event extends Analysis. Filter Portfolio { protected Portfolio p; Balance Filter public boolean handle. Data. Event (Data. Feed. Event dfe, Alert. List al) { boolean consumed = false; // issue/remove alerts based on // data feed event and projected // risk/gain to portfolio 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 Analysis. Pipeline list alerts { private Composite. Filter cf; private Data. Feed df; private Alert. List al; data public void add. Filter event Operator (Analysis. Filter af) {cf. add. Filter (af); } public void send. Alerts () {/* Send all alerts, reset list */} Filter public void run () Pipeline { for (; ; ) { Data. Feed. Event dfe = df. pull. Data. Feed. Event (); cf. handle. Data. Event (dfe, al); // possibly long latency send. Alerts (); /* latency depends on alert count */} } } Christopher D. Gill
Real-Time Java Programming Example: Stock Market Analysis Tool // Analysis Tool Code public class Analysis. Tool { public static void main (String [] args) { Analysis. Pipeline ap = new Analysis. Pipeline (); ap. add. Filter (new Portfolio. Balance. Filter ()); ap. add. Filter (new Sector. PEFilter ()); ap. run (); // run the pipeline } } Christopher D. Gill alert list send alerts market order data event data feed Market
Real-Time Java Programming Analysis. Tool Review: Roadmap Analysis. Pipeline Analysis. Filter Alert. List Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event Nasdaq. Data. Feed Portfolio. Balance. Filter Annotation Research. Annotation Portfolio Annotation. List Market. Order. Alert Option. Alert Data. Store Nasdaq. Annotation Call. Alert Nasdaq. Store Research. Store Buy. Alert Christopher D. Gill Sell. Alert Put. Alert
Real-Time Java Programming Analysis. Tool Example: Time Scales Analysis. Pipeline Analysis. Filter Alert. List Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event Nasdaq. Data. Feed Portfolio. Balance. Filter Annotation Research. Annotation Portfolio Annotation. List Market. Order. Alert Option. Alert Data. Store Nasdaq. Annotation Call. Alert Nasdaq. Store Latency: Research. Store Low Christopher D. Gill Medium High Buy. Alert Sell. Alert Put. Alert
Real-Time Java Programming Java Real-Time Issues • Existing Java. TM 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 Java. TM”) Christopher D. Gill
Real-Time Java Programming Java: Threading Issues • Separate threads of execution are useful to // Separate high latency activity improve liveness by doing the public class Store. Thread. Adapter following concurrently: implements Runnable – Getting and handling { private Data. Store store; market data events private Alert alert; public Store. Thread. Adapter • Medium latency (Data. Store ds, Alert a) – Searching stores to add { store = ds; alert = a; } annotations public void run () { • High latency store. annotate. Alert (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 Sector. PEFilter extends Analysis. Filter { private Nasdaq. Store nh; private Research. Store rr; public boolean handle. Data. Event (Data. Feed. Event dfe, Alert. List al) { boolean consumed = false; // possibly generate new alerts. . . //. . . annotated with relevant research reports. . . Thread annotation. Thread = new Thread (new Store. Thread. Adapter (rr, alert)); annotation. Thread. set. Priority (Thread. MIN_PRIORITY); annotation. Thread. start (); return consumed; } } Christopher D. Gill
Real-Time Java Programming Java: Threading Issues // Analysis Tool Code, Revisited // Separate low latency activity public class Alert. Thread. Adapter implements Runnable { private Analysis. Pipeline pipeline; private long timeout; public Alert. Thread. Adapter (Analysis. Pipeline ap, long t) { pipeline = ap; timeout = t; } public void run () { for (; ; ) // in reality, could use more sophisticated { // loop control e. g. , wait, notify. All, etc. try { Thread. sleep (timeout); pipeline. send. Alerts (); } catch (java. lang. Interrupted. Exception e) {/*. . . */} } Christopher D. Gill
Real-Time Java Programming Java: Threading Issues // Analysis Pipeline Code, Revisited // Separate medium latency activity public class Analysis. Pipeline { private Composite. Filter cf; // filters in the pipeline private Data. Feed df; // paced data event feed private Alert. List al; // list of alerts public void add. Filter (Analysis. Filter af) {cf. add. Filter (af); } public void send. Alerts () {/* Send all alerts in the list, reset alert list */} public void run () { for (; ; ) { Data. Feed. Event dfe = df. pull. Data. Feed. Event (); cf. handle. Data. Event (dfe, al); // possibly long latency } } } Christopher D. Gill
Real-Time Java Programming Java: Threading Issues // Analysis Tool Code, Revisited public class Analysis. Tool { public static void main (String [] args) { Analysis. Pipeline ap = new Analysis. Pipeline (); ap. add. Filter (new Portfolio. Balance. Filter ()); ap. add. Filter (new Sector. PEFilter ()); Thread alert. Thread = new Thread (new Alert. Thread. Adapter (ap, 1000)); alert. Thread. set. Priority (Thread. MAX_PRIORITY); alert. Thread. 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 add. Annotation (Annotation a) {/*. . . */} public synchronized Annotation next. Annotation (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 finegrain synchronization (Lea, “Concurrent Programming in Java. TM”) • We’ll combine two styles: block and method synchronization
Real-Time Java Programming Java: Synchronization Issues // Concurrency safety additions using block synchronization public class Analysis. Pipeline { /*. . . */ protected void send. Alerts () { synchronized (al) {/* Send all the alerts in the list, reset alert list */} } public void run () { for (; ; ) { Data. Feed. Event dfe = df. pull. Data. Feed. Event (); // spawns separate threads for long latency activities cf. handle. Data. Event (dfe, al); } } Christopher D. Gill
Real-Time Java Programming Java: Synchronization Issues // Concurrency safety additions using block synchronization public class Portfolio. Balance. Filter extends Analysis. Filter { protected Portfolio p; public boolean handle. Data. Event (Data. Feed. Event dfe, Alert. List 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 Sector. PEFilter extends Analysis. Filter { private Nasdaq. Store nh; private Research. Store rr; public boolean handle. Data. Event (Data. Feed. Event dfe, Alert. List 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 Analysis. Tool Threads and Synch Points medium Analysis. Pipeline Analysis. Filter latency Alert. List low latency Composite. Filter Data. Feed. Event Nasdaq. Data. Feed Data. Store Sector. PEFilter Portfolio. Balance. Filter Annotation Research. Annotation high latency Portfolio Annotation. List Market. Order. Alert Option. Alert Nasdaq. Annotation Call. Alert Nasdaq. Store Research. Store Synchronization points: Christopher D. Gill Buy. Alert Sell. Alert Put. Alert
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 // Solution: real-time threads • Threads compete for time on the CPU Alert. Thread. Adapter alert. Adapter = • Some activities are higher new Alert. Thread. Adapter (ap, 1000); priority than others javax. realtime. Realtime. Thread • Java thread priorities take alert. Thread = new us a step in the right javax. realtime. Realtime. Thread direction, but… (alert. Adapter); – garbage collector javax. realtime. Realtime. Thread thread priority and pipeline. Thread = preemption issues new javax. realtime. Realtime. Thread (ap); – Non-RT priority uniqueness is not alert. Thread. start (); ensured pipeline. Thread. start (); Christopher D. Gill
Real-Time Java Programming RTSJ: Threading Issues // To run the pipeline in a Realtime thread, it could just implement Runnable: for Analysis. Pipeline this is not very invasive so we’ll skip writing a separate adapter public class Analysis. Pipeline implements Runnable { /*. . . */ protected void send. Alerts () { synchronized (al) {/* Send all the alerts in the list, reset alert list */} } public void run () { for (; ; ) { Data. Feed. Event dfe = df. pull. Data. Feed. Event (); // spawns separate threads for long latency activities cf. handle. Data. Event (dfe, al); } } Christopher D. Gill
Real-Time Java Programming RT Issues: Release Characteristics execution cost • To know whether threads will interfere, need to characterize their temporal behavior period deadline minimum inter-arrival spacing Time Christopher D. Gill • 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. Relative. Time cost = • new javax. realtime. Relative. Time (100, 0); While threading allows priority partitioning, specific information javax. realtime. Relative. Time period = and/or constraints on new javax. realtime. Relative. Time (1000, 0); threads are needed javax. realtime. Periodic. Parameters pp = new • Must ensure sufficient javax. realtime. Periodic. Parameters ( resources are available null, // start immediately, and correctly managed period, cost, for desired behavior null, // deadline = period end null, null); alert. Thread. set. Release. Parameters (pp); alert. Thread. start (); Christopher D. Gill
Real-Time Java Programming RTSJ: Release Characteristics Issues // Analysis Tool Code, Revisited public class Alert. Thread. Adapter implements javax. realtime. Schedulable { /* we can & should get/set release parameters, scheduling parameters, memory parameters, . . . */ public void run () {add. To. Feasibility (); javax. realtime. Realtime. Thread t = (javax. realtime. Realtime. Thread) Thread. current. Thread (); for (; ; ) { t. wait. For. Next. Period (); // respect advertised cost, period times pipeline. send. Alerts (); } } } 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 Cost. Overrun. Event. Handler extends javax. realtime. Async. Event. Handler • { public void handle. Async. Event() {/*. . . */}} public class Deadline. Miss. Event. Handler extends javax. realtime. Async. Event. Handler { public void handle. Async. Event() {/*. . . */}} javax. realtime. Periodic. Parameters pp = new javax. realtime. Periodic. Parameters (null, // start immediately, period, cost, null, // deadline = period end new Cost. Overrun. Event. Handler (), new Deadline. Miss. Event. Handler ()); alert. Thread. set. Release. Parameters (pp); alert. Adapter. set. Release. Parameters (pp); alert. Thread. 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. Priority. Scheduler psched = (javax. realtime. Priority. Scheduler) javax. realtime. Scheduler. get. Default. Scheduler (); javax. realtime. Priority. Parameters high = new javax. realtime. Priority. Parameters (psched. get. Max. Priority ()); • javax. realtime. Priority. Parameters med = new javax. realtime. Priority. Parameters (psched. get. Norm. Priority ()); try { alert. Thread. set. Scheduling. Parameters (high); • pipeline. Thread. set. Scheduling. Parameters (med); } catch (java. lang. Illegal. Argument. Exception e) {/*. . . */} • alert. Thread. start (); pipeline. Thread. 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 Store. Thread. Adapter implements javax. realtime. Schedulable {/*. . . */ public void run () { javax. realtime. Priority. Scheduler psched = (javax. realtime. Priority. Scheduler) javax. realtime. Scheduler. get. Default. Scheduler (); try { javax. realtime. Priority. Parameters pp = new javax. realtime. Priority. Parameters (psched. get. Min. Priority ()); set. Scheduling. Parameters (pp); javax. realtime. Realtime. Thread t = (javax. realtime. Realtime. Thread) Thread. current. Thread (); t. set. Scheduling. Parameters (pp); } catch (java. lang. Illegal. Argument. Exception e) {/*. . . */} store. annotate. Alert (alert); } } Christopher D. Gill
Real-Time Java Programming RT Issues: Synchronization running outside block synchronized 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. Monitor. Control. set. Monitor. Control (new javax. realtime. Priority. Inheritance ()); • // Solution: wait-free queues public class Store. Thread. Adapter implements javax. realtime. Schedulable { • /*. . . */ private javax. realtime. Wait. Free. Dequeue 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 notify. All 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 Store. Timeout. Handler • extends javax. realtime. Async. Event. Handler {public void handle. Async. Event() {/*. . . */}} • public class Store. Thread. Adapter implements javax. realtime. Schedulable { public void run () { //. . . set up thread priorities. . . long m = 60000; // one minute • new javax. realtime. One. Shot. Timer (new javax. realtime. Relative. Time (m, 0), new Store. Timeout. Handler ()); store. annotate. Alert (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 event – Real-world timing issues – 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 Transmit. Timeout. Handler extends javax. realtime. Async. Event. Handler {public void handle. Async. Event () {/*. . . */}} new javax. realtime. Periodic. Timer (null, new javax. realtime. Relative. Time (1000, 0), new Transmit. Timeout. Handler ()); 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. Memory. Area ma = new javax. realtime. LTMemory (init. Size, max. Size); javax. realtime. No. Heap. Realtime. Thread alert. Thread = new javax. realtime. No. Heap. Realtime. Thread (sp, // sched params rp, // release params mp, // memory params ma, // memory area pg, // processing group alert. Adapter); 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 • No. Heap. Realtime threads avoid this
Real-Time Java Programming RTSJ: Memory Management Issues • Scoped memory is key // Immortal Memory is a Singleton for no-heap real-time threads javax. realtime. Memory. Area im = javax. realtime. Immortal. Memory. instance (); • Other kinds of Memory. Area im. enter (this); // this must be Runnable – Immortal Memory: // allocates memory on can improve GC // the Immortal. Memory area performance // until another memory // area is entered, or • Physical Memory // 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 Data. Store { /*. . . */ public abstract void annotate. Alert (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. Asynchronously. Interrupted. Exception; } // In timer handling for // Store. Thread. Adapter 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 Wait. Free. Queue, 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 get. Manager set. Manager Christopher D. Gill security manager • Although strict layering is often desirable, platform-specific 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. Realtime. System is analogous to java. lang. System – Gives access to real-time system properties • E. g. , concurrent locks, endian properties – Allows a Realtime. Security manager to be set as the system security manager – Gives access to the current garbage collector • Posix. Signal. Handler – Required on platforms that provide POSIX signals – Thus, can only be used portably among those implementations Christopher D. Gill
Real-Time Java Programming Analysis. Tool Review: Time Scales Analysis. Pipeline Analysis. Filter Alert. List Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event Nasdaq. Data. Feed Portfolio. Balance. Filter Annotation Research. Annotation Portfolio Annotation. List Market. Order. Alert Option. Alert Data. Store Nasdaq. Annotation Call. Alert Nasdaq. Store Latency: Research. Store Low Christopher D. Gill Medium High Buy. Alert Sell. Alert Put. Alert
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 Analysis. Tool Review: Java and RTSJ medium duration timer Analysis. Pipeline Analysis. Filter Composite. Filter Data. Feed Sector. PEFilter Data. Feed. Event over-run handler feasibile low latency high priority Portfolio. Balance. Filter Portfolio Annotation. List latency Alert. List real-time periodic no heap scoped Alert memory priority inheritance Nasdaq. Data. Feed Data. Store Nasdaq. Store Research. Annotation high latency aynch transfer of control Market. Order. Alert Nasdaq. Annotation Call. Alert Research. Store Synchronization points: Christopher D. Gill Option. Alert Buy. Alert Sell. Alert Put. Alert
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