programming r-t abstractions tsw november 2009 anders p. ravn aalborg university

32
Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Programming R-T Abstractions

TSW November 2009Anders P. Ravn

Aalborg University

Page 2: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Characteristics of a RTS

• Timing Constraints• Concurrent control of separate components• Dependability Requirements • Facilities to interact

with special purpose hardware

Page 3: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Timing Constraints

• Notion of time• Specifying timing constraints:

Temporal scopes• Notion of Event• Clocks, delays and timeouts

Page 4: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RT Java Time Types

public abstract class HighResolutionTime implements java.lang.Comparable{ ... public boolean equals(HighResolutionTime time); public final long getMilliseconds(); public final int getNanoseconds();

public void set(HighResolutionTime time); public void set(long millis); public void set(long millis, int nanos);}

Page 5: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Time

HighResolutionTime

AbsoluteTime RelativeTime

Page 6: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

public class AbsoluteTime extends HighResolutionTime{ // various constructor methods including public AbsoluteTime(AbsoluteTime T); public AbsoluteTime(long millis, int nanos); ... public final AbsoluteTime add(RelativeTime time); public final RelativeTime subtract(AbsoluteTime time); public final AbsoluteTime subtract(RelativeTime time);}

RTSJ Absolute and Relative Time

public class RelativeTime extends HighResolutionTime{ // various constructor methods including public RelativeTime(long millis, int nanos); public RelativeTime(RelativeTime time); ... public final RelativeTime add(RelativeTime time); public final RelativeTime subtract(RelativeTime time);}

Page 7: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Temporal scope

• C: maximum execution time• D: deadline for completion of execution• T: minimum time between releases (period)• S: minimum delay before start of execution

Page 8: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ ReleaseParameters

ReleaseParameters

PeriodicParameters AperiodicParameters

SporadicParameters

Page 9: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ ReleaseParameters

public abstract class ReleaseParameters { protected ReleaseParameters( RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler); ...}

Page 10: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

public class PeriodicParameters extends ReleaseParameters{ public PeriodicParameters( HighResolutionTime start, RelativeTime period, RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler);

public RelativeTime getPeriod(); public HighResolutionTime getStart();}

RTSJ Periodic Parameters

Page 11: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ Aperiodic- and SporadicParameterspublic class AperiodicParameters extends ReleaseParameters{ public AperiodicParameters(RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler);}

public class SporadicParameters extends AperiodicParameters{ public SporadicParameters(RelativeTime minInterarrival, RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler);

public RelativeTime getMinimumInterarrival(); public void setMinimumInterarrival(RelativeTime minimum);}

Page 12: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ AsyncEventpublic class AsyncEvent{ public AsyncEvent(); ... public void addHandler(AsyncEventHandler handler);

public void fire(); ...}

An asynchronous event can have a set of handlers associated with it, and when the event occurs, the fireCount of each handler is incremented, and the handlers are released.

Page 13: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ Clockpublic abstract class Clock{ public Clock();

public static Clock getRealtimeClock();

public abstract RelativeTime getEpochOffset();

public AbsoluteTime getTime(); public abstract void getTime(AbsoluteTime time);

public abstract RelativeTime getResolution(); public abstract void setResolution(RelativeTime resolution);

}

Page 14: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Characteristics of a RTS

• Timing Constraints• Concurrent control of separate components• Dependability Requirements • Facilities to interact

with special purpose hardware

Page 15: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ Schedulable

«interface»Schedulable

AsyncEventHandler

BoundAsyncEventHandler

RealTimeThread

NoHeapRealTimeThread

Page 16: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ AsyncEventHandler

public class AsyncEventHandler extends java.lang.Object implements Schedulable { public AsyncEventHandler( SchedulingParameters scheduling, ReleaseParameters release, MemoryArea area); ... public void handleAsyncEvent(); // the program to be executed ... }

Page 17: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ RealTimeThreadpublic class RealtimeThread extends java.lang.Thread implements Schedulable { public RealtimeThread(SchedulingParameters s, ReleaseParameters r); . . .

. . . public static RealtimeThread currentRealtimeThread();

public synchronized void schedulePeriodic(); // add the thread to the list of schedulable objects public synchronized void deschedulePeriodic(); // remove the thread from the list of schedulable object // when it next issues a waitForNextPeriod public boolean waitForNextPeriod() throws ...;

public synchronized void interrupt(); // overrides java.lang.Thread.interrupt()

public static void sleep(Clock c, HighResolutionTime time) throws ...; }

Page 18: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Ravenscar Periodic Thread

Thread MemoryArea

+run() : void

«interface»Runnable

«interface»Schedulable

RealtimeThread

NoHeapRealTimeThread

-End1

1

-End2

1

-aJileTh : com.ajile.jem.PeriodicThread

PeriodicThreadInitializer

PriorityParameters

-End1

1

-End2

1

PeriodicParameters

+run() : void

«interface»Runnable

-End1

1

-End2

1

-End1

1 -End2

1

Page 19: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Asynchronous Event and Handler

#bindTo(in happening : String)#addHandler(in handler : AsyncEventHandler)#fire()

AsyncEvent

+fire()

SporadicEvent SporadicInterrupt

BoundAsyncEventHandler

#handleAsyncEvent()+run()

AsyncEventHandler

+handleAsyncEvent()

SporadicEventHandler

NoHeapRealTimeThread

RelativeTime

-End1

1

-End2

1

-End1

1

-End2

1

uses

+run()

«interface»Runnable

«interface»Schedulable

uses

Page 20: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

A Real-Time Application

• Periodic Event Handlers• Aperiodic Event Handlers

collected in a mission• Mission Handler

Each handler has a Memory is Scheduled

20

Page 21: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Periodic handlerclass Periodic extends PeriodicEventHandler{ protected Periodic(.., PeriodicParameters pp, Scheduler scheduler, MemoryArea memory);

public void handleEvent() { // the logic to be executed every period }}

21

Page 22: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Aperiodic handlerclass Aperiodic extends AperiodicEventHandler{ protected Aperiodic(.., AperiodicParameters ap, Scheduler scheduler, MemoryArea memory);

public void handleEvent() { // the logic to be executed when an event occurs }}

22

Page 23: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

A simple missionpublic class Basic extends Mission{ protected Basic(.., AperiodicParameters ap, Scheduler scheduler, MemoryArea memoryArea) {

... // initialization }

public static void main (String[] args) { new Basic( null, null, new CyclicScheduler(), new LTMemory(10*1024)); }}

23

Page 24: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

…The mission

addToMission( new Periodic( null, pp, getScheduler(), new LTMemory(1024)));addToMission( new Periodic( null, pp, getScheduler(), new LTMemory(1024)));...add(); // mission to its scheduler

24

Page 25: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Complex missionprivate Mission[] mission;private int active = 0;static AperiodicEvent event;

public ThreeSequentialMissions(...) { mission = new Mission[3];

// set up the three missions mission[0] = new Mission(...); // add handlers for mission 0 // including the mission termination ... mission[1] = new Mission(); ... // start the first mission mission[active].add(); event = new AperiodicEvent(this);}

25

Page 26: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Changing mission

private Mission[] mission;private int active = 0;static AperiodicEvent event;

public ThreeSequentialMissions(...) { ...}

public void handleEvent() { mission[active].remove(); active = (active + 1) % mission.length; mission[active].add();}

26

Page 27: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ Scheduler

Scheduler

PriorityScheduler

Class which represents the required (by the RTSJ) priority-based scheduler. The default instance is the base scheduler which does fixed priority, preemptive scheduling.

Page 28: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ SchedulingParameters

public class PriorityParameters { public PriorityParameters(int priority); ... }

public class ImportanceParameters { public PriorityParameters(int priority, int importance); ... }

Importance is an additional scheduling metric that may be used by some priority-based scheduling algorithms during overload conditions to differentiate execution order among threads of the same priority.

Page 29: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ PriorityScheduler

public class PriorityScheduler extends Scheduler{ public static PriorityScheduler instance(); ... protected boolean addToFeasibility(Schedulable schedulable); public boolean isFeasible();

public boolean SetIfFeasible( Schedulable schedulable, ReleaseParameters release, MemoryParameters memory);}

Page 30: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ Memory Management

MemoryArea

«singleton»HeapMemory

ScopedMemory

ImmortalMemory

LTMemoryImmortalPhysicalMemory

VTMemory

Page 31: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

RTSJ MemoryAreapublic abstract class MemoryArea { protected MemoryArea(long sizeInBytes);

public void enter(java.lang.Runnable logic); // associate this memory area to the current thread // for the duration of the logic.run method

public static MemoryArea getMemoryArea(java.lang.Object object); // get the memory area associated with the object

public long memoryConsumed(); // number of bytes consumed in this memory area public long memoryRemaining(); // number of bytes remaining

. . .

public synchronized java.lang.Object newInstance( java.lang.Class type)throws IllegalAccessException, InstantiationException, OutOfMemoryError; // allocate an object

public long size(); // the size of the memory area}

Page 32: Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University

Simplicity – Static Schedules

• Cyclic Executive• Time Triggered Architecture (Kopetz)• Synchronous Languages (Esterel)• Giotto (Henzinger)