RealtimeThread - School of Computer Science

Download Report

Transcript RealtimeThread - School of Computer Science

G53SRP: Real Time Threads
in RTSJ (part I)
Chris Greenhalgh
School of Computer Science
1
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)
2
Class Overview
<<interface>>
java.lang.
Runnable
implements
extends
<<interface>>
javax.realtime.
Schedulable
java.lang.
Thread
implements
<<abstract>>
javax.realtime.
Scheduler
extends
javax.realtime.
RealtimeThread
extends
javax.realtime.
NoHeapRealtimeThread
has
extends
…
javax.realtime.
PriorityScheduler
(Default)
3
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
4
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 int getMinPriority();
public int getMaxPriority(); // at least min+27
public int getNormPriority();
// other PriorityScheduler methods
…
}
See HelloScheduler.java
5
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
6
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);
7
…
public static RealtimeThread currentRealtimeThread();
// 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, int nanos);
static public void sleep(long millis);
static public void sleep(long millis, int nanos);
// high-res versions
static void sleep(Clock clock, HighResolutionTime t);
static void sleep(HighResolutionTime time);
…
8
…
// scheduler
public Scheduler getScheduler();
public void setScheduler(Scheduler scheduler);
// from Thread
public void setPriority(int priority);
public int getPriority();
// getters & setters for all parameters
// (release, scheduler, memory, proc.group)…
…
// periodic threads (see later)
static boolean waitForNextPeriod();
static boolean waitForNextPeriodInterruptible();
public void schedulePeriodic();
public void deschedulePeriodic();
…
}
9
E.g. HelloRealtimeThread.java
import javax.realtime.RealtimeThread;
public class HelloRealtimeThread extends RealtimeThread
{
Code run with realtime support
public void run() {
RealtimeThread rt =
RealtimeThread.currentRealtimeThread();
System.out.println("Hello from "+rt);
}
public static void main(String args[]) {
RealtimeThread rt = new HelloRealtimeThread();
rt.start();
try { rt.join(); }
catch (InterruptedException ie) { }
}
10
}
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
extends
javax.realtime.
PriorityParameters
extends
javax.realtime.
ImportanceParameters
Empty class
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
See NRTPriority.java
– 11-? (at least 28)
– Strict order – only highest priority runs
See RTPriority.java
ImportanceParameters
• Extends PriorityParameters
• Adds an integer importance property
• May be used by scheduler in deadline overrun situations
– E.g. to identify safety-critical tasks for
preferential scheduling
• Optional – depends on scheduler
implementation
17
ReleaseParameters classes
<<abstract>>
javax.realtime.
ReleaseParameters
extends
javax.realtime.
PeriodicParameters
javax.realtime.
AperiodicParameters
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
package javax.realtime;
public class PeriodicParameters
extends ReleaseParameters {
public PeriodicParameters(
Specific to Periodic tasks
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
Example Periodic
RealtimeThread (no problems)
Process Release Time
Process
Start
Period…
Process Completion Time
Deadline met
Call to waitForNextPeriod
RT1
Actual
task execution
waitForNextPeriod blocking
0
1000 2000 3000 4000 5000
Time (ms)
24
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)
25
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
26
Summary (3)
– PeriodicParameters
• Defines thread start time/delay and period
• Supported by
RealtimeThread.waitForNextPeriod
27