270 likes | 287 Vues
G53SRP: Real Time Threads in RTSJ (part I). Chris Greenhalgh School of Computer Science. Contents. Overview The Scheduler Class Fixed Priority Scheduler The RealtimeThread Class RealtimeThread properties SchedulingParameters PriorityParameters ReleaseParameters
E N D
G53SRP: Real Time Threads in RTSJ (part I) Chris Greenhalgh School of Computer Science
Contents • Overview • The Scheduler Class • Fixed Priority Scheduler • The RealtimeThread Class • RealtimeThread properties • SchedulingParameters • PriorityParameters • ReleaseParameters • Periodic RealtimeThreads overview • Book: Wellings 10.2.1, 10.3, 10.4.1, 10.4.2 (part), 10.4.4, 10.5, 12.1 (part), 12.3 (part)
Class Overview <<interface>>java.lang.Runnable implements extends <<interface>>javax.realtime.Schedulable java.lang.Thread <<abstract>>javax.realtime.Scheduler extends implements has javax.realtime.RealtimeThread extends javax.realtime.PriorityScheduler … extends (Default) javax.realtime.NoHeapRealtimeThread
Checking and setting the Scheduler package javax.realtime; public abstract class Scheduler { public static Scheduler getDefaultScheduler(); public static void setDefaultScheduler(Scheduler s); public String getPolicyName(); // other Scheduler methods … } See HelloScheduler.java
The Priority-based Scheduler package javax.realtime; public class PriorityScheduler extends Scheduler { public static PriorityScheduler instance(); // Scheduler-related info: priorities // Note: all RT Priorities > standard (1-10) public intgetMinPriority(); public intgetMaxPriority(); // at least min+27 public intgetNormPriority(); // other PriorityScheduler methods … } See HelloScheduler.java
The Priority-based Scheduler • Only standard scheduler specified in RTSJ • Used as initial default scheduler • Schedules realtime threads according to priority • Single fixed priority assigned to each thread • Fully pre-emptive • i.e. a higher priority thread that becomes runnable immediately pre-empts a lower priority thread • Does not time-slice threads of equal priority • Running thread may yield to allow another equal-priority thread to run
The RealtimeThread class package javax.realtime; public class RealtimeThread extends java.lang.Thread implements Schedulable { public RealtimeThread( SchedulingParameters scheduling, ReleaseParameters release, MemoryParameters memory, MemoryArea area, ProcessingGroupParameters group, java.lang.Runnable logic); // partial cons. – defaults are all null public RealtimeThread(); public RealtimeThread(SchedulingParameters s); public RealtimeThread(SchedulingParameters s, ReleaseParameters release); …
… public static RealtimeThreadcurrentRealtimeThread(); // from Thread public void run(); // runs logic by default public void start(); public void interrupt(); // changed: see ATC public void join(); public void join(long millis); public void join(long millis, intnanos); static public void sleep(long millis); static public void sleep(long millis, intnanos); // high-res versions static void sleep(Clock clock, HighResolutionTime t); static void sleep(HighResolutionTime time); …
… // scheduler public Scheduler getScheduler(); public void setScheduler(Scheduler scheduler); // from Thread public void setPriority(int priority); public intgetPriority(); // getters & setters for all parameters // (release, scheduler, memory, proc.group)… … // periodic threads (see later) static booleanwaitForNextPeriod(); static booleanwaitForNextPeriodInterruptible(); public void schedulePeriodic(); public void deschedulePeriodic(); … }
E.g. HelloRealtimeThread.java import javax.realtime.RealtimeThread; public class HelloRealtimeThread extends RealtimeThread { public void run() { RealtimeThreadrt = RealtimeThread.currentRealtimeThread(); System.out.println("Hello from "+rt); } public static void main(String args[]) { RealtimeThreadrt = new HelloRealtimeThread(); rt.start(); try { rt.join(); } catch (InterruptedExceptionie) { } } } Code run with realtime support
RealtimeThread (Schedulable) properties • SchedulingParameters (subclass) • Scheduler-specific thread information, e.g. priority • ReleaseParameters (subclass) • Temporal characteristics of the thread, e.g. release time, period, deadline, cost • MemoryParameters • memory usage of thread, e.g. max size, max rate of allocation
RealtimeThread (Schedulable) properties (cont.) • MemoryArea • type/area of memory in which new objects will be allocated by thread • ProcessingGroupParameters • optional group limits (cost per period) for a set of schedulable objects • java.lang.Runnable • the logic to be run by the default run method (or over-ride run method)
SchedulingParameters classes <<abstract>>javax.realtime.SchedulingParameters Empty class extends javax.realtime.PriorityParameters extends javax.realtime.ImportanceParameters
PriorityParameters class package javax.realtime; public abstract class PriorityParameters extends SchedulingParameters { public PriorityParameters(int priority); public int getPriority(); public void setPriority(int priority); } 14
PriorityParameters notes • SchedulingParameters depend on Scheduler used • PriorityParameters used with (e.g.) PriorityScheduler • Equivalent to Thread get/setPriority
Priority notes • Realtime Priorities > normal thread priorities • Normal priorities • 1-10 • Time-sliced – priority affects time budget • RT priorities • 11-? (at least 28) • Strict order – only highest priority runs See NRTPriority.java See RTPriority.java
ImportanceParameters • Extends PriorityParameters • Adds an integer importance property • May be used by scheduler in deadline over-run situations • E.g. to identify safety-critical tasks for preferential scheduling • Optional – depends on scheduler implementation
ReleaseParameters classes <<abstract>>javax.realtime.ReleaseParameters extends javax.realtime.AperiodicParameters javax.realtime.PeriodicParameters extends javax.realtime.SporadicParameters
ReleaseParameters class package javax.realtime; public abstract class ReleaseParameters { protected ReleaseParameters( RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler); // getters & setters … } 19
ReleaseParameters notes • For every type of realtime thread or other Schedulable • cost • Time units per release • May (or may not) be enforced as maximum • May (or may not) be used for feasibility test • deadline • Latest completion time after release time
PeriodicParameters class Specific to Periodic tasks package javax.realtime; public class PeriodicParameters extends ReleaseParameters { public PeriodicParameters( HighResolutionTime start, RelativeTime period, RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler); // getters & setters … } 21
PeriodicParameters notes • start may be AbsoluteTime or RelativeTime • Default deadline = period
Periodic RealtimeThread • Follows standard code pattern: public void run() { boolean ok = true; while(ok) { // periodic task code … ok = RealtimeThread. waitForNextPeriod(); } // contingency… } See HelloPeriodicThread.java
0 1000 2000 3000 4000 5000 Example Periodic RealtimeThread (no problems) Process Release Time Process Process Completion Time Deadline met Start Period… Call to waitForNextPeriod RT1 Actual task execution waitForNextPeriod blocking Time (ms)
Summary • Scheduler exposed through Scheduler abstract class • Fixed Priority Scheduler provided as default –PriorityScheduler • RealtimeThread class extends java.lang.Thread • implements Schedulable as well as Runnable (see later)
Summary (2) • RealtimeThread properties • SchedulingParameters – for Scheduler • PriorityParameters – for PriorityScheduler, specifies thread priority (like Thread.get/setPriority) • ReleaseParameters • Defines thread deadline and cost • Subclassed as PeriodicParameters, AperiodicParameters & SporadicParameters
Summary (3) • PeriodicParameters • Defines thread start time/delay and period • Supported by RealtimeThread.waitForNextPeriod