Document 7480832
Download
Report
Transcript Document 7480832
Physical Clocks
Topics
Physical Clocks
Clock Synchronization Algorithms
Readings
Van Steen and Tanenbaum: 5.1
Coulouris: 10.3
Introduction
Electronic clocks in most servers and
network devices keep inaccurate time.
Small errors can add up over a long period.
Assume two clocks are synchronized on
January 1.
One of the clocks consistently takes an extra
0.04 milliseconds to increment itself by a
second.
On December 31 the clocks will differ by 20
minutes
Introduction
In some instances it is acceptable to
measure time with some accuracy:
When we try to determine how many minutes
left in an exam
Making a soft boiled egg
We can be relaxed about time in some
instances:
The time it takes to drive to Toronto
The number of hours studied for an exam
Introduction
However, for many applications more
precision is needed.
Example: Telecommunications
Accurate timing is needed to ensure that the
switches routing digital signals through their
networks all run at the same rate.
If not, slow running switches would not be able
to cope with traffic and messages would be
lost.
Introduction
Example: Global Positioning System (GPS)
Ship, airplane and car navigation use GPS to
determine location.
GPS satellites that orbit Earth broadcast
timing signals from their clocks.
By looking at the signal from four (or more)
satellites, the user’s position can be
determined.
Any tiny error could put you off course by a
very long way.
• A nanosecond of error translates into a GPS error of
one foot.
Introduction
Other:
Need to know when a transaction occurs
Equipment on a factory floor may need to know
when to turn on or off equipment.
Billing services
E-mail sorting can be difficult if time stamps
are incorrect
Tracking security breaches
Secure document transmissions
Clock Synchronization
In a centralized system:
Time
is unambiguous. A process gets the time
by issuing a system call to the kernel. If
process A gets the time and later process B
gets the time then the value B gets is higher
than (or possibly equal to) the value A got.
Example: UNIX make examines the times at
which all the source and object files were last
modified.
• If time (input.c) > time(input.o) then recompile input.c
• If time (input.c) < time(input.o) then no compilation is
needed.
Clock Synchronization
In a distributed system, achieving agreement on
time is not easy.
Assume no global agreement on time. Let’s see
what happens:
Assume that the compiler and editor are on
different machines
output.o has time 2144
output.c is modified but is assigned time 2143
because the clock on its machine is slightly
behind.
Make will not call the compiler.
The resulting executable will have a mixture of
object files from old and new sources.
Clock Synchronization
When each machine has its own clock, an event
that occurred after another event may
nevertheless be assigned an earlier time.
Clock Synchronization
Another example
File synchronization after disconnected
operation
• Synchronize workstation and laptop copies of 402a1.c
• Disconnect laptop
• Make some changes to 402-a1.c on the laptop.
• Reconnect and re-sync, hopefully copying laptop
version over the workstation version.
If
laptop’s clock is behind workstation, the copy
might go the other way around
Clocks
Clocks should be synchronized. The
question is this: Is it possible to do so?
A computer has a timer, not a clock.
A timer is a precisely machined quartz
crystal oscillating at a frequency that
depends on how the crystal was cut and the
amount of tension.
Clocks
Two registers are associated with the crystal:
a counter and a holding register.
Each
oscillation of the crystal decrements the
counter by one. When counter is 0, an interrupt is
generated.
Each interrupt is called a clock tick.
At each clock tick, the interrupt procedure adds
one to the time stored in memory. The counter is
reinitialized by the value in the holding register.
A timer can be programmed to generate an
interrupt n times per second.
Clocks
On each of the computers, the crystals will
run at slightly different frequencies, causing
the software clocks gradually to get out of
sync. This is called clock skew or clock drift.
Ordinary quartz clocks drift by ~ 1 sec in 1112 days. (10-6 secs/sec).
High precision quartz clocks drift rate is ~
10-7 or 10-8 secs/sec
Physical Clocks
Two problems:
How
do we synchronize computer clocks with
real-world clocks?
How do we synchronize the computer clocks
with each other?
To answer these questions, we have to
understand how time is measured.
Physical Clocks
Since the invention of mechanical clocks
(17th century) time is measured
astronomically (mean solar day, mean solar
second).
The event of the sun’s reaching its highest
apparent point is called transit of the sun.
This event occurs at about noon each day.
The interval between two consecutive
transits of the sun is called the solar day.
Physical Clocks
There are 24 hours in a day
Each hour contains 3600 seconds.
Each day contains 86400 seconds
The solar second is defined as exactly
1/86400th of a solar day.
Physical Clocks
Computation of the mean solar day.
Physical Clocks
In the 1940’s it was established that the
period of the earth’s rotation is not
constant.
It is believed that 300 million years ago
there were about 400 days per year.
The length of the year (the time for one
trip around the sun) is not thought to have
changed; the day has simply become longer.
Short term variations in the length of the
day also occur.
Physical Clocks
Astronomers now compute the length of the day by
measuring a large number of days and taking the
average before dividing by 86,400. This is called the
mean solar second.
The invention of the atomic clock in 1948 made it
easier to measure time more accurately.
This is done by counting the transitions (changing
energy state) of the cesium 133 atom. A second is
defined as the time it takes the cesium 133 atom to
make exactly 9,192,631,770 transitions.
Physical Clocks
The atomic time is computed to make the
atomic second equal to the mean solar
second (in the year of its introduction in
1958).
Currently about 50 labs have cesium 133
clocks.
Each of them periodically tells the BIH
(Bureau International d l’Heure) in Paris
how many times its clock ticked.
BIH averages these values. This is
referred to as International Atomic Time
(TAI).
Universal Coordinated Time
TAI is stable and available to anyone who
wants to go to the trouble of buying a
cesium clock.
Problem:86,400 TAI seconds is now about
3 msec less than a mean solar day since a
solar day is getting longer.
Using TAI means that over the course of
years, noon would get earlier and earlier,
until it would eventually occur in the wee
hours of the morning.
Universal Coordinated Time
Since the mean solar day gets longer, the
BIH made necessary corrections ( by
introducing leap seconds) resulting in
Universal Coordinated Time (UTC).
UTC is provided to those who need precise
time:
National
Institute of Standard Time (NIST)
operates a shortwave radio station with call
letters WWV from Fort Collins, Colorado with +1
to –1 msec accuracy.
Earth satellites also offer a UTC service: accurate
to 0.5 msec
By telephone from NIST: cheaper but less
accurate.
Need to compensate for signal propagation delay.
Physical Clock Coordination
If one machine has a WWV receiver, the goal
becomes keeping all the other machines
synchronized to it.
If no machines have WWV receivers, each
machine keeps track of its own time; the goal
is to keep the machines synchronized.
Physical Clock Synchronization
Model of system assumed by clock
synchronization algorithms:
When UTC is t, the value of the clock on
machine p is Cp(t).
In a perfect world, Cp(t) = t for all p and all t;.
dC/dt denotes the ratio of the change in the
clock time to actual time.
• In “perfect world” dC/dt should be one
Physical Clock Coordination
The relation between clock time and UTC when clocks
tick at different rates.
Physical Clock Coordination
Each clock has a maximum drift rate .
1- dC/dt <= 1+
In time t a clock may drift t
Assume two clocks are synchronized at time t.
Assume that the clocks drift the maximum
possible in t in opposite directions. In t, they
are2t apart.
To limit drift to
2t=
t= /(2).
resynchronize every d/2 seconds
Christian’s Algorithm
One time server (WWV) receiver; all other
machines stay synchronized with the time
server.
Periodically ( no more than /2 seconds),
each machine sends a message to the time
server asking for the current time.
Time server machine responds with CUTC
Cristian's Algorithm
Getting the current time from a time server.
Christian’s Algorithm
CUTC could be smaller then requestor’s
clock. Must not set the clock backwards
(would this work for the makefile
example?).
Suppose that a timer is set to generate 100
interrupts per second.
Each interrupt would add 10 msec to the time.
Instead the interrupt routine could add only 9
msec each time until the correction has been
made.
A clock can be advanced gradually by adding 11
msec at each interrupt.
Christian’s Algorithm
Problem with client setting the clock to
CUTC
It takes a nonzero amount of time for the time
server’s reply to get back to the sender.
The delay may be large and it varies with
network load.
T0: Starting time of the client’s request
T1: be the time that the client receives an
answer (measured using the same clock).
The best estimate of the message passing
propagation time is this: (T1-T0)/2
The new time should be CUTC + (T1-T0)/2
Christian’s Algorithm
Problem with client setting the clock to
CUTC (continued)
Also should take into account time it takes the
time server to handle the interrupt and process
the incoming message. This is the interrupt
handle time, I.
The new time should be based on T1-T0 -I
Christian suggested sending several messages
and the fastest reply would be the most
accurate since it presumably encountered the
least traffic.
Christian’s Algorithm
Single server may fail and thus render
synchronization impossible (on a temporary
basis).
Christian suggests a group of synchronized
time servers, each with a receiver for UTC
time signals.
A client multicasts its request to all
servers and uses only the first reply
obtained.
Christian’s Algorithm
A faulty time server or an imposter time
server that replied with deliberately
incorrect times, could wreak havoc in a
computer systems.
It has been shown that if f is the number
of faulty clocks out of a total of N, then
we must have N > 3f.
The problem with faulty clocks is partially
addressed by the Berkeley algorithm.
Berkeley Algorithm
Suitable when no machine has a WWV receiver.
The time server (daemon) is active:
Time daemon polls every machine periodically to ask
what time is there
Based on the answers, it computes an average time
Tells all other machines to advance their clocks to the
new time or slow their clocks down until some specified
reduction has been achieved.
Propagation is taken into account.
The time daemon’s time is set manually by
operator periodically.
The Berkeley Algorithm
a)
b)
c)
The time daemon asks all the other machines for their clock
values using a polling mechanism and providing “current time”
The machines answer indicating how they differ from time sent.
The time daemon tells everyone how to adjust their clock based
on a calculation of the “average time value”.
The Berkeley Algorithm
The master takes a fault-tolerant average.
A subset of clocks is chosen that do not differ
from one another by more than a specified
amount and the average is taken of readings
from only these clocks.
Should the master fail, then another can
be elected (discussed later) using one of
the election algorithms.
Network Time Protocols (NTP)
Christian’s method and Berkeley algorithm
intended for intranets
NTP intended to provide the ability to
externally synchronize clients across the
Internet to UTC.
Network Time Protocol (NTP)
The NTP service is provided by a network
of servers located across the Internet.
Primary servers receive UTC.
Secondary servers are synchronized with
primary servers.
The servers are connected in a logical
hierarchy called a synchronization subnet
whose levels are called strata.
An example synchronization
subnet in an NTP
implementation
1
2
3
2
3
3
Note: Arrows denote synchronization control, numbers denote
strata.
Network Time Protocols (NTP)
Layered client-server architecture, based
on UDP message passing.
The clocks belonging to servers with high
stratum numbers are liable to be less
accurate than those with low stratum
numbers since errors are introduced at
each level of synchronization.
If a strata 1 server fails, it may become a
strata 2 server that is being synchronized
through another strata 1 server.
Accuracy: range of 1-50 msec
Network Time Protocols (NTP)
A 1999 survey of NTP servers shows the
following:
175,000 servers running NTP in the Internet
300 are stratum 1 servers
Over 20,000 are stratum 2 servers
Over 80,000 are stratum 3 servers
Using Coordination Algorithms
Cristian’s method and the Berkeley
algorithm are intended primarily for use
within an intranet.
NTP is intended for the Internet.
Is it Enough to Synchronize
Physical Clocks?
Values received by a UTC receiver is
accurate within a range.
At best we can synchronize clocks to within
10-30 milliseconds of each other.
We have to synchronize frequently, to
avoid clock drift.
The synchronization algorithms are good,
but not always sufficient.
Is it Enough to Synchronize
Physical Clocks?
Replication of data and processing is an approach
often used for reliability and performance
reasons.
Reliability
If one replica is unavailable or crashes, use another
Avoid single points of failure
Performance
Placing copies of data close to the processes using them
can improve performance through reduction of access
time.
If there is only one copy, then the server could become
overloaded.
Is it Enough to Synchronize
Physical Clocks?
Problems with replication include:
Whenever a copy is modified, that copy
becomes different from the rest.
Modifications have to be carried out on all
copies to ensure consistency.
Assume that update 1 is issued at 10:00
and update 2 is issued at 10:00:0001.
Due to network delays, replica 2 receives
update 2 before update 1.
How is replica 2 to know that there is an earlier
update?