Lập trình thời gian
thực trong Java
Đồng hồ
và
lập lịch
Clocks and Time
Lecture aims
To provide some background on clocks and time
To explain absolute and relative time values
To consider the support for real-time clocks
To give some simple examples
To review the parameter classes in detail
To consider alternative schedulers
2
Vũ
Quang
Dũng
Introduction I
Standard Java only supports the notion of a wall clock (calendar
time); for many applications, a clock based on UTC (Coordinated
Universal Time) is sufficient
However real-time systems often require
A monotonic clock which progresses at a constant rate and is not
subject to the insertion of extra ticks to reflect leap seconds (as UTC
clocks are). A constant rate is needed for control algorithms which want
to executed on a regular basis. Many monotonic clocks are also relative
to system startup and can be used only to measure the passage of time,
not calendar time
A count down clock which can be paused, continued or reset (for
example the clock which counts down to the launch of the Space
Shuttle)
A CPU execution time clock which measures the amount of CPU time
that is being consumed by a particular thread or object
3
Vũ
Quang
Dũng
Introduction II
All of the above clocks also need a resolution which is
potentially finer than the millisecond level
They may all be based on the same underlying physical
clock, or be supported by separate clocks
Where more than one clock is provided, issues of the
relationship between them may be importance; in
particular, whether their values can drift apart
We consider the additional clock and time classes that
are provided by the RTSJ to augment the standard Java
facilities
4
Vũ
Quang
Dũng
Basic Model
The RTSJ defines a hierarchy of time classes rooted on
the abstract HighResolutionTime class
This abstract class has three subclasses:
one which represents absolute time
one which represents relative time
and one which represents rational time
The intention is to allow support for time values down to
the nanosecond accuracy
Clocks are supported through an abstract Clock class
5
Vũ
Quang
Dũng
High Resolution Time I
6
Vũ
Quang
Dũng
public abstract class HighResolutionTime
implements Comparable, Cloneable
{
// methods
public abstract AbsoluteTime absolute(Clock clock);
public int compareTo(HighResolutionTime time);
public boolean equals(HighResolutionTime time);
public final long getMilliseconds();
public final int getNanoseconds();
public abstract RelativeTime relative(Clock clock);
public void set(HighResolutionTime time);
public void set(long millis, int nanos);
public static void waitForObject(Object target,
HighResolutionTime time) throws InterruptedException;
}
High Resolution Time II
The abstract methods (absolute and relative) allow time types that are
relative to be re-expressed as absolute time values and vice versa.
The methods also allow the clocks associated with the values to be changed.
absolute to absolute
The value returned has the same millisecond and nanosecond components as the
encapsulated time value
absolute to relative
The value returned is the value of the encapsulated absolute time minus the current time
as measured from the given clock parameter
relative to relative
The value returned has the same millisecond and nanosecond components as the
encapsulated time value
relative to absolute
The value returned is the value of current time as measured from the given clock
parameter plus the encapsulated relative time
7
Vũ
Quang
Dũng
High Resolution Time III
Changing the clock associated with a time value is
potentially unsafe, particularly for absolute time values
This is because absolute time values are represented as a
number of milliseconds and nanoseconds since an epoch
Different clocks may have different epochs.
The waitForObject does not resolve the problem of
determining if the schedulable object was woken by a
notify method call or by a timeout
It does allow both relative and absolute time values to be
specified.
8
Vũ
Quang
Dũng
Absolute Time I
9
Vũ
Quang
Dũng
public class AbsoluteTime extends HighResolutionTime
{
// constructors
public AbsoluteTime();
public AbsoluteTime(AbsoluteTime time);
public AbsoluteTime(java.util.Date date);
public AbsoluteTime(long millis, int nanos);
// methods
}
Absolute Time II
10
Vũ
Quang
Dũng
public class AbsoluteTime extends HighResolutionTime
{
public AbsoluteTime absolute(Clock clock);
public AbsoluteTime add(long millis, int nanos);
public final AbsoluteTime add(RelativeTime time);
public java.util.Date getDate();
public RelativeTime relative(Clock clock);
public void set(java.util.Date date);
public RelativeTime subtract(AbsoluteTime time);
public AbsoluteTime subtract(RelativeTime time);
public String toString();
}
Relative Time I
11
Vũ
Quang
Dũng
public class RelativeTime extends HighResolutionTime
{
// constructors
public RelativeTime();
public RelativeTime(long millis, int nanos);
public RelativeTime(RelativeTime time);
}
Relative Time II
12
Vũ
Quang
Dũng
public class RelativeTime extends HighResolutionTime
{
// methods
public AbsoluteTime absolute(Clock clock);
public RelativeTime add(long millis, int nanos);
public RelativeTime add(RelativeTime time);
public RelativeTime relative(Clock clock);
public RelativeTime subtract(RelativeTime time);
public String toString();
}
Clocks
The RTSJ Clock class defines the abstract class from which all clocks are
derived
The language allows many different types of clocks; eg, an execution-time
clock which measures the amount of execution time being consumed
There is real-time clock which advances monotonically
can never go backwards
should progress uniformly and not be subject to the insertion of
leap ticks
The static method getRealtimeClock allows this clock to be obtained
Other methods are provided to get the resolution of a clock and, if the
hardware permits, to set the resolution of a clock
13
Vũ
Quang
Dũng
The Clock Class
14
Vũ
Quang
Dũng
public abstract class Clock
{
// constructor
public Clock();
// methods
public static Clock getRealtimeClock();
public abstract RelativeTime getResolution();
public AbsoluteTime getTime();
public abstract void getTime(AbsoluteTime time);
public abstract void setResolution(
RelativeTime resolution);
}
Example: measuring elapse time
15
Vũ
Quang
Dũng
{
AbsoluteTime oldTime, newTime;
RelativeTime interval;
Clock clock = Clock.getRealtimeClock();
oldTime = clock.getTime();
// other computations
newTime = clock.getTime();
interval = newTime.subtract(oldTime);
}
Example: A launch clock
A launch clock is clock which is initiated with a relative time value
and an absolute time value
The absolute time value is the time at which the clock is to start
ticking; the relative time value is the duration of the countdown
The count down can be stopped, restarted, or reset
The class extends the Thread class
The constructor saves the start time, the duration and the clock to
be used
The resolution of the count down is one second, and various
functions allow the current launch time to be queried
16
Vũ
Quang
Dũng
LaunchClock
I
17
Vũ
Quang
Dũng
public class LaunchClock extends Thread {
public LaunchClock(AbsoluteTime at,
RelativeTime countDown) {
super();
startTime = at;
remainingTime = countDown;
myClock = Clock.getRealtimeClock();
counting = true;
go = false;
tick = new RelativeTime(1000,0);
}
private AbsoluteTime startTime;
private RelativeTime remainingTime;
private Clock myClock;
private boolean counting;
private boolean go;
private RelativeTime tick;
LaunchClock
II
18
Vũ
Quang
Dũng
public RelativeTime getResolution()
{
return tick;
}
public synchronized AbsoluteTime
getCurrentLaunchTime()
{
return new AbsoluteTime(
myClock.getTime().add(remainingTime));
// assumes started and ticking
}
LaunchClock
III
19
Vũ
Quang
Dũng
public synchronized void stopCountDown()
{
counting = false;
notifyAll();
}
public synchronized void restartCountDown()
{
counting = true;
notifyAll();
}
public synchronized void resetCountDown(
RelativeTime to)
{
remainingTime = to;
}
LaunchClock
IV
20
Vũ
Quang
Dũng
public synchronized void launch() throws Exception
{
while(!go){
try {
wait();
} catch(InterruptedException ie) {
throw new Exception("Launch failed");
}
}
// Launch is go
}
LaunchClock
V
21
Vũ
Quang
Dũng
public void run()
{
try {
synchronized(this) {
while(myClock.getTime().compareTo(startTime) < 0)
HighResolutionTime.
waitForObject(this, startTime);
while(remainingTime.getMilliseconds() > 0) {
while(!counting) wait();
HighResolutionTime.waitForObject(this, tick);
remainingTime.set(
remainingTime.getMilliseconds() -
tick.getMilliseconds());
}
go = true;
notifyAll();
}
}
catch(InterruptedException ie) { }
}
}
Scheduling and Schedulable
Objects
To give an overview of fixed priority
schedule
To present the RTSJ Basic Scheduling
Model
22
Vũ
Quang
Dũng
Introduction
Real-time systems must be able to interact with their
environment in a timely and predictable manner
Designers must engineer analysable systems whose
timing properties can be predicted and mathematically
proven correct
Scheduling is the ordering of thread/process executions
so that the underlying hardware resources (processors,
networks, etc.) and software resources (shared data
objects) are efficiently and predictably used
23
Vũ
Quang
Dũng
Scheduling
In general, scheduling consists of three components
an algorithm for ordering access to resources (scheduling policy)
an algorithm for allocating the resources (scheduling
mechanism)
a means of predicting the worst-case behaviour of the system
when the policy and mechanism are applied (schedulability
analysis or feasibility analysis)
Once the worst-case behaviour of the system has been
predicted, it can be compared with the system’s timing
requirements to ensure that all deadlines will be met
24
Vũ
Quang
Dũng
Fixed Priority Scheduling: Policy
FPS requires
statically allocating schedulable objects to processors
ordering the execution of schedulable objects on a single
processor according to a priority
assigning priorities to schedulable objects at their creation time
— although no particular priority assignment algorithm is
mandated by FPS, it is usual to assign priorities according to the
relative deadline of the schedulable object (relative to the
schedulable object’s release time); the shorter the deadline, the
higher the priority
priority inheritance when accessing resources
25
Vũ
Quang
Dũng